PHP - Mysql Fetch Array
Hi,
I'm wondering if theres a shortcut to a potential problem I have. I'm currently running a query on my website to pull all the fields from a table in my database, for the data to be used on various parts of the page. Usually I would do something as follows Code: [Select] $result = mysql_query("SELECT * FROM table WHERE page='1'"); while ($row=mysql_fetch_array($result)) { $title = $row["title"]; $data = $row["data"]; } And so on and so forth. I would then call the appropriate data by echoing $data for example. However, my table contains a lot more rows than i've mentioned (Around 25 or so). Rather than assigning each to a variable and having a large portion of variable assignments at the top of the page, is there any clever way of putting all of these values inside of an array. So for example, I could call $array_data["title"] or $array_data["data"]?? So it keeps the same key, but puts it inside of an array that I don't have to loop through each time? Hope that makes sense! Thanks, Edd Similar TutorialsI've been using this code for a long time and realised it's very repetitive, but the id_column I want changes all the time function getRows() { $query = mysql_query($sql); $rows = array(); while($row = mysql_fetch_assoc($query)) { $rows[$row['id_column']] = $row; } return $rows; } So I wrote this function that will automatically create an array with a column I choose if I wish, but I'm not sure if it's very efficient. function getRows($query, $column_id = false) { $rows = array(); while($row = mysql_fetch_assoc($result)) { if($column_id === false) { $rows[] = $row; } else { if(isset($row[$column_id])) { $rows[$row[$column_id]] = $row; } else { $rows[] = $row; } } } return $rows; } I would appreciate some input as to make it better. Thanks. Hi,
Does anyone have a clue how I might solve this little issue:
I have a MySQL query, for example:
<?php // Make a MySQL Connection $query = "SELECT * FROM staff"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ if($row['clock'] < time()) { echo "<b>"$row['name']. " - ". $row['age']"</b>"; echo "<br />"; } else { echo $row['name']. " - ". $row['age']; echo "<br />"; } } ?>Taking data from the following table setup: name - age - clock Timmy Mellowman - 23 - 09:00:00 Sandy Smith - 21 - 12:00:00 Bobby Wallace - 15 - 14:00:00 What im trying to achieve is compare the current time to the time in the clock column and depending if it's earlier or later, change the styling of the results. The above code does appear to work somewhat however it seems to change the styling of all results in one go, rather than individually when it passes the time in the cell, which is what im looking for. Thanks Warning: mysql_fetch_array() expects parameter 1 to be resource, string given... im tryin to put class inside code if $dayNumber and $dan are equal i tried few ways but all are complicated since i have to call some functions this end up as "simple" solution but i dont know where did i get wrong PS that should be possible? Code: [Select] else { echo "<td width='40'"; $moj_id = $_SESSION['id']; $query_event = mysql_query("SELECT * FROM events WHERE id_user='$moj_id' GROUP BY 'event_date'") ; $test = mysql_fetch_array('$query_event'); $date_event = $test['event_date']; sscanf($date_event, "%d-%d-%d", $godina, $mjesec, $dan); if ($dan==$dayNumber && $mjesec==$month && godina==$year) { echo " class='radni'"; } echo"><a href='day_info.php?dan=",$dayNumber,"&mj=",$month,"&gd=",$year,"'>" . $dayNumber . " </a></td>\n"; $dayNumber++; } Okay, this is getting on my nerves now, my head is throbbing probably just need a break from it all now. I'm currently getting the following message; Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\duff3\test3.php on line 25 Can anyone figure out and explain why this isn't working for me, I believe I am calling the function correctly and then putting the result into a while loop. Code: [Select] <?php function getPageContent($selectedPage) { $sql = "SELECT * "; $sql .= "FROM tbl_pages "; $sql .= "WHERE tbl_pages.pageCategoryId = ".$selectedPage." "; $sql .= "AND active = 1 "; $sql .= "LIMIT 1"; $result = mysql_query($sql) or die ("Error in page sql query:". $sql); return $pageSet; } ?> Code: [Select] <?php require_once ''.$_SERVER['DOCUMENT_ROOT'].'/duff3/commonResources/dbConnection/dbQueryClass.php'; ?> <?php #FUNCTIONS IS A TEMP FILE UNTIL OO PHP COMES INTO PLAY require_once ("commonResources/includes/functions.php"); # if page isn't set then set it to default. if(isset($_GET["page"])) { $selectedPage = $_GET["page"]; } else { $selectedPage = 1; } #call function to select all page $pageSet = getPageContent($selectedPage); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> <?php while ($row = mysql_fetch_array($pageSet)) { # Start while loop echo $row["pageTitle"]; ?> </title> <link rel="shortcut icon" href="commonResources/images/container/favicon.ico" /> <link rel="stylesheet" type="text/css" href="<?php $_SERVER["DOCUMENT_ROOT"] ?>/duff3/commonResources/css/style.css" /> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" /> <!-- PUT COMMON JAVASCRIPT FILES IN AN INCLUDE 05/05/2011 --> <script type="text/javascript" src="commonResources/javaScript/jQuery.js"></script> <script type="text/javascript" src="commonResources/javaScript/jQueryTest/menu.js"></script> <?php require_once ("".$_SERVER["DOCUMENT_ROOT"]."/duff3/commonResources/includes/tinymce.php"); ?> </head> <body> <div id="viewContainer"><!--OPEN DIV FOR VIEW CONTAINER --> <div id="headerContent"><!--OPEN DIV FOR HEADER CONTENT --> <div class="logoContent"><!--OPEN DIV FOR LOGO CONTAINER --> <a href="index.html"><img src="<?php $_SERVER["DOCUMENT_ROOT"] ?>/duff3/commonResources/images/container/logo.png" alt="Hannah Jane Duff" longdesc="Hannah Jane Duff Home Link" /></a> </div><!--CLOSE DIV FOR LOGO CONTAINER --> </div><!--CLOSE DIV FOR HEADER CONTENT --> <!-- TEMP TAKE OUT AND PUT INTO HEADER AREA --> <div id="mainContent"><!--OPEN DIV FOR MAIN CONTENT--> <div class="centreContent"><!--OPEN DIV FOR CENTRE CONTENT--> <div id="menuContent"><!--OPEN DIV FOR MENU CONTENT--> <div id="menu"> <ul class="menu"> <li><a href="index.html" class="main"><span>Home</span></a></li> <li><a href="" class="main"><span>bio</span></a> <div><ul> <li><a href=""><span>violin</span></a></li> <li><a href=""><span>piano</span></a></li> <li><a href=""><span>singing</span></a></li> <li><a href="" class="parent"><span>teaching</span></a> <div><ul> <li><a href="" class="parent"><span>aberdeen</span></a> <div><ul> <li><a href="#"><span>aberdeen 1</span></a></li> </ul></div> </li> <li><a href="" class="parent"><span>bradford</span></a> <div><ul> <li><a href=""><span>bradford 1</span></a></li> </ul></div> </li> <li><a href="" class="parent"><span>leeds</span></a> <div><ul> <li><a href=""><span>leeds 1</span></a></li> </ul></div> </li> </ul></div> </li> <li><a href="" class="parent"><span>influences</span></a> <div><ul> <li><a href=""><span>classical</span></a></li> <li><a href=""><span>folk</span></a></li> </ul></div> </li> <li><a href="" class="parent"><span>other</span></a> <div><ul> <li><a href=""><span>folk′d</span></a></li> <li><a href=""><span>string quaret</span></a></li> </ul></div> </li> </ul></div> </li> <li><a href="" class="main"><span>publicity</span></a> <div><ul> <li><a href="" class="parent"><span>news</span></a> <div><ul> <li><a href=""><span>may 2011</span></a></li> <li><a href=""><span>march 2011</span></a></li> </ul></div> </li> <li><a href=""><span>gallery</span></a></li> <li><a href=""><span>other</span></a></li> </ul></div> </li> <li><a href="" class="main"><span>recordings</span></a> <div><ul> <li><a href=""><span>all</span></a></li> <li><a href=""><span>new york dolls</span></a></li> <li><a href=""><span>classical</span></a></li> </ul></div> </li> <li><a href="" class="main"><span>contact</span></a></li> </ul></div> </div><!--CLOSE DIV FOR MENU CONTENT--> <div class="paraBlock"><!--OPEN DIV FOR PARA BLOCK --> <p><?php echo $row["pageContent"]; ?></p> </div><!--CLOSE DIV FOR PARA BLOCK--> <div class="clearArea"><!--OPEN DIV FOR CLEAR AREA--> </div><!--CLOSE DIV FOR CLEAR AREA--> </div><!-- CLOSE DIV FOR CENTRE CONTENT--> </div><!--CLOSE DIV FOR MAIN CONTENT--> <div id="footerContent"><!--OPEN DIV FOR FOOTER CONTENT--> <div class="leftFooter"><!--OPEN DIV FOR LEFT FOOTER--> © <?php echo date(Y); ?> All Rights Reserved | Design by <a href="http://www.innovationation.co.uk/">Innovationation UK</a> </div><!--CLOSE DIV FOR LEFT FOOTER--> <div class="rightFooter"><!--OPEN DIV FOR RIGHT FOOTER--> <a href="">Copyright | </a> <a href="">Disclaimer | </a> <a href="">Privacy Policy </a> <a href="">Terms of Use | </a> <a href="">Site Map | </a> <a href="loginArea/login.php">Admin</a> </div><!--CLOSE DIV FOR RIGHT FOOTER--> </div><!--CLOSE DIV FOR FOOTER CONTENT--> <?php } ?> </div><!--CLOSE DIV FOR VIEW CONTAINER--> </body> </html> Having problem with a bit of code. I am trying to fetch data from the database, I have used mysql_fetch_array before, and even in later bits of code. I want to use this data for an IF statement later on. The IF statement isn't there, because this code is catching somewhere. The fetch array is working fine, and it echos out the correct row in database that it should. The issue, it stops displaying code that follows it. In the code, I have put a comment (// Code kills here ....) where the code is killing. I have identified the few lines of code that is killing the script. You can see the page.. The number 71 is what is supposed to be supplied by the query and array. This is the page as it is with the trouble code. Here is the page without the trouble bit of code, and it displays all that it should. But it doesn't have the functionality I want to build in with the array obviously. Here is the code with highlight bit of trouble: /source/userinfo.php Code: [Select] <?php include ("db.php"); $get_info = mysql_query ("SELECT * FROM registrations WHERE u_id=$uid AND appid=$appid" , $link2); //Begin Trouble Code while ($check = mysql_fetch_array($get_info)) { echo $check['assign_id']; } //End Trouble Code // Code kills here, for some reason.... Proper assign_id displayed, but it kills script. $count = mysql_num_rows($get_info); if ($count >= 1) { $get_jos_info = mysql_query ("SELECT * FROM jos_users WHERE id=$appid" , $link); $get_avatar = mysql_query ("SELECT * FROM jos_comprofiler WHERE user_id=$appid" , $link); while ($avatar_link = mysql_fetch_array($get_avatar)) $avatarurl = $avatar_link['avatar']; echo $avatar_link['avatar']; echo "<table border='0' width='70%' align='center' cellpadding=5>"; while ($rowteam = mysql_fetch_array($get_info)) { while ($rowjos = mysql_fetch_array($get_jos_info)) { $urlbegin = "<img src='http://versionxlegends.com/images/comprofiler/tn"; $urlend = "'>"; echo "<tr><td width=100% align=center colspan=2>".$urlbegin.$avatarurl.$urlend."</td></tr>"; echo $avatar; echo "<tr><td width=40% align=right><strong>Registration #</strong></td><td width=60% align=center>$rowjos[id]</td></tr>"; echo "<tr><td width=40% align=right><strong>User's Username</strong></td><td width=60% align=center>$rowteam[username]</td></tr>"; echo "<tr><td width=40% align=right><strong>User's Name</strong></td><td width=60% align=center>$rowteam[name]</td></tr>"; echo "<tr><td width=40% align=right><strong>Provided Skype Username</strong></td><td width=60% align=center>$rowteam[skype]</td></tr>"; //Add script for lead only view Email Address. Team Member, field is hidden. Lead sees email and a YES/NO if email matches. If not, lead will email user to get email verification, to edit account before assignments occur. echo "<tr><td width=40% align=right><strong>E-Mail Address</strong></td><td width=60% align=center>$rowteam[email]</td></tr>"; echo "<tr><td width=40% align=right><strong>Registration Date</strong></td><td width=60% align=center>$rowjos[registerDate]</td></tr>"; // Add script for lead only view age. Team Member, field is hidden. Lead sees birthdate and/or age and a YES/NO if age matches. If not, lead will email user with ID verification e-mail, to verify age before assignments occur. echo "<tr><td width=40% align=right><strong>Age</strong></td><td width=60% align=center>$rowteam[age]</td></tr>"; }} echo "</table>"; } else { echo "Invalid URL. Registration does not exist"; } ?> This code is included within this page: viewregistration.php Code: [Select] <?php session_start(); $user_id = $_SESSION['user_id']; $uid = $_GET['u_id']; $appid= $_GET['appid']; include ("db.php"); $get = mysql_query ("SELECT * FROM registrations WHERE u_id = $uid AND appid=$appid", $link2); echo $get; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" /> <title>Nuke Ice By ThemeKings</title> <link rel="stylesheet" type="text/css" href="nuke-ice.css" /> </head> <body> <div id="wrapper"> <div id="banner"> <div id="banner-edit"></div> </div> <div id="container"> <?php include ("sidebar.php"); ?> <div id="col-2"> <?php $uid = $_GET['uid']; $appid= $_GET['appid']; ?> <div class="CntBox"> <div class="CntHead"> <div class="CntHeadInfo">User Information</div> </div> <div class="CntFill"> <div class="CntInfo"> <?php include ("./source/userinfo.php"); ?> </div> </div> <div class="CntFooter"></div> </div> I included this bit of code so you can see some of the variables that were assigned in another file. Hi all, I'm new to PHP have written some code to extract data from mysql. here it is <!DOCTYPE html> <html lang="en"> result 1.png 18.34KB 0 downloads result 2.png 24.62KB 0 downloads Table.png 4.61KB 0 downloads <head> <meta charset="utf-8" /> <title></title> </head> <body> <?php echo "Testing Database....<br>"; $con = mysqli_connect('localhost', 'db_id', 'my_pw', 'mysql_db'); $result = $con->query("SELECT user_id FROM users"); echo "Number of users : ".$result->num_rows."<br><br>"; $row = mysqli_fetch_row($result); echo $row[0]."<br>"; echo $row[1]."<br>"; echo $row[2]."<br>"; ?> </body> </html> in result number of rows its giving right, but when i fetch the result in row, its giving only first row. rest all are empty. then further i have tested few more things like, i change the query as below 'SELECT * FROM users' now this is giving another strange result. It selected first row of the table and making array for all available columns. i have attached herewith image of my table, and result of both of my queries. Could someone guide me on this issue... Im not sure where to post this but since it includes php il post it here instead of in the mysql forum. ok so, i have a table and i get the values using while($row = mysql_fetch_array($result)){ and then echo them in rows. that works fine but i need to add a class to the last row of my table. I would need somehow to fetch the last row of the array and make it echo something different. Any help is appreciated Thank you <?php require("connection.php"); $query = 'SELECT * FROM job_post WHERE id_jobpost= :Id'; $start = $bdd->prepare($query); $start->execute(array(':Id' => $_GET['id_jobpost'])); $result = $start->fetch(); foreach ($result as $results) { $jobtitle = $result['jobtitle']; $id_jobpost = $result['id_jobpost']; $description = $result['description']; } ?> https://prnt.sc/uk3913 Whats wrong with this code Hi, I am trying to fetch all data from database. but any data show twice. like this: Ali Ali 30 30 Osman Osman 20 20 Omer Omer 40 40 Salim Salim 10 10 Suzan Suzan 25 25 $mem_qry = "SELECT * FROM tbl_name"; $res = mysql_query($mem_qry); function mysql_fetch_all($res) { $return = array(); while($row=mysql_fetch_array($res)) { $return[] = $row; } return $return; } $all = mysql_fetch_all($res); foreach($all as $k=>$v) { foreach($v as $k2=>$v2){ echo $v2.'<br />'; } } Basically, I have the following code ($c2 is my connection variable): Code: [Select] $rid = $_GET['id']; $q = mysql_query("SELECT * FROM reports WHERE id = $rid", $c2) or die(mysql_error()); $report = mysql_fetch_array($q); $report is used later on to gather more information that is outputted to the user. However, if in the URL, someone were to put id=1', they would have an error message spit out to them (something along the lines of: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'' at line 1), indicating a SQL Injection exploit. How would I go about fixing this, and also preventing SQL Injection? Thanks a bunch, Mark Hi guys, Im having problem on how to achieved this one.. I would like to get the all event of this month (April ) Starting todays date.. Code: (php) [Select] $now = gmdate("Y-m-d", mktime(gmdate("H")+8, gmdate("i"), gmdate("s"), gmdate("m"), gmdate("d")+8, gmdate("Y"))); $week = gmdate("Y-m-d", mktime(gmdate("H")+8, gmdate("i"), gmdate("s"), gmdate("m"), gmdate("d")+30, gmdate("Y"))); if ($result = mysql_query("SELECT * FROM fiesta WHERE sta_date BETWEEN '$now' AND '$week' ORDER BY sta_date LIMIT 10")){ if (mysql_num_rows($result)) { $return = ''; while ($row = mysql_fetch_array( $result )) { $date = date("D",strtotime($row["sta_date"])); $return .= "<a href='sta.php' style='font-size:11px;' title='" . $row['fiesta_brgy'] . " " . $row['fiesta_town'] . "'>" . $row['fiesta_brgy'] . " " . $row['fiesta_town'] . "</a> ($date), "; } echo rtrim($return, ', '); } } The result of this query will include the events on May.. How can i display only the event on this month(April)? starting todays date. Thank you Hello i want to fetch the content from my MySQL db for one table and show it on a page, i'm using this code: Code: [Select] <?php include("inc/connect.php"); ?> <?php $result = $_REQUEST['result']; ?> Code: [Select] <?php $query = mysql_query("select * from listtest order by id asc"); while($result = mysql_fetch_array($query)) { ?> <tr> <td><?php echo $result['id']; ?></td> <td><?php echo $result['code']; ?></td> <td align="center"><a href="edit.php?id=<?php echo $result['id']; ?>">View</a></td> <td align="center"> </td> </tr> <?php } ?> it's throwing me this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in 32qgtf.php on line 30 What am i doing wrong ? /Edit: line 29/30 is the mysql query / fetch. Hey guys tried to search for this but didn't find anything usefull about the issue. So what i have is mssql_fetch_array in a while loop in the loop i have it assign a specified row as a variable that i can print later on in the page. Code: [Select] while($row = mssql_fetch_array($query) { $tables = "<tr>"; $tables .= "<td> {$row['specifiedrow']} </td>"; $tables .= "</tr>"; }Then i call it in the html table as Code: [Select] <? php echo $tables; ?> I have done this with mysql_fetch_array before and it would print the entire database row associated with that row in the table. But as it is now it only returns the last row from the database. Is there something i am missing. Thanks for any help Hi, I've used this code before, but have had to make some modifications and am now getting a mysql fetch array error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/tesolcla/public_html/test/results10k201010.php on line 134 If anyone can help, it would be much appreciated. Code: [Select] <?php $dbcnx = @mysql_connect('localhost', 'MYUSERNAME', 'MYPASSWORD'); //$dbcnx = @mysql_connect('localhost', 'root', 'mysql'); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } if (!@mysql_select_db('MYDATABASE')) { //if (!@mysql_select_db('rac')) { exit('<p>Unable to locate the results ' . 'database at this time.</p>'); } $asc_on = '<img src="images/results_sorting/Asc.gif" border="0" />'; $asc_off= '<img src="images/results_sorting/AscOff.gif" border="0" />'; $desc_on = '<img src="images/results_sorting/Desc.gif" border="0" />'; $desc_off= '<img src="images/results_sorting/DescOff.gif" border="0" />'; $sortfield = isset($_GET['sort']) ? $_GET['sort'] : '4'; $sorttype = isset($_GET['type']) ? $_GET['type'] : '1'; for($i=1; $i<5; $i++) { if($i==$sortfield) { if ($sorttype==1) $srt[$i] = $asc_on.'<a href="?sort='.$i.'&type=2">'.$desc_off.'</a>'; else $srt[$i] = '<a href="?sort='.$i.'&type=1">'.$asc_off.'</a>'.$desc_on; } else { $srt[$i] = '<a href="?sort='.$i.'&type=1">'.$asc_off.'</a><a href="?sort='.$i.'&type=2">'.$desc_off.'</a>'; } } $fields = array("firstname", "lastname", "time", "position"); $sorts = array("ASC", "DESC"); $field = $fields[$sortfield-1]; $sort = $sorts[$sorttype-1]; $field = $field=="" ? $fields[4] : $field; $sort = $sort=="" ? $sorts[0] : $sort; $sql = mysql_query("SELECT firstname, lastname, time, position FROM 10k_results ORDER BY $field $sort"); echo "<table border='1' align='center' bordercolor='#000000' CELLPADDING=5 cellspacing='0' STYLE='font-size:13px'>"; echo "<tr bgcolor='#008000' STYLE='color:white'> <td>*</td><td><H3>First name $srt[1]</h3></td> <td><H3>Lastname $srt[2]</H3></td> <td><H3>Time $srt[3]</H3></td> <td><H3>Position $srt[4]</H3></td></tr>"; // keeps getting the next row until there are no more to get $row_counter = 1; //create row counter with default value 0 // Print out the contents of each row into a table while ($row = mysql_fetch_array($sql)) { // Print out the contents of each row into a table echo "<tr>\n"; echo "</td><td>"; echo $row_counter++; echo "</td>"; echo "<td>{$row['firstname']}</td>\n"; echo "<td>{$row['lastname']}</td>\n"; echo "<td>{$row['time']}</td>\n"; echo "<td>{$row['position']}</td>\n"; echo "</tr>\n"; } echo "</table>"; ?> //my controller <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; class homeController extends Controller { public function index() { $employee = DB::table('employee')->orderBy('id','desc')->get(); $department = DB::table('department')->orderBy('id','desc')->get(); return view('index', ['employee' => $employee , 'department' => $department]); } } //my routes Route::get('index','homeController@index'); //my view using blade temmplating engine @foreach($employee as $emp) <div class="employee"> <b>{{ $emp->name }} </b> <a href="employee/{{ $emp->id }}"> <p class="intro">{{ substr($emp->intro ,0, 50) }}...</p> </a> </div> @endforeach @foreach($department as $dep) <div class="department"> <b>{{ $dep->name }} </b> <a href="department/{{ $dep->id }}"> <p class="desc">{{ substr($dep->description ,0, 100) }}...</p> </a> </div> @endforeach I want to fetch using ajax, how can i do it, teach/help me Hello, I have this simple php form that updates the table, and also shows what the information in it is. Here it is: <strong>Update multiple rows in mysql</strong><br> <?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="test_mysql"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); // Count table rows $count=mysql_num_rows($result); ?> <table width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="post" action=""> <tr> <td> <table width="500" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>Id</strong></td> <td align="center"><strong>Month</strong> <strong>Date</strong> </td> <td align="center"><strong>Lastname</strong></td> <td align="center"><strong>Email</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td> <td align="center"><input name="month[]" MAXLENGTH="3" size="3" type="text" id="month" value="<?php echo $rows['month']; ?>">- <input name="date[]" MAXLENGTH="2" size="2" type="text" id="date" value="<?php echo $rows['date']; ?>"> </td> <td align="center"><input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>"></td> <td align="center"><input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>"></td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> </table> <?php // Check if button name "Submit" is active, do this if (isset($_POST['Submit'])) { for($i=0;$i<$count;$i++){ $month = $_POST['month']; $date = $_POST['date']; $lastname = $_POST['lastname']; $email = $_POST['email']; $name = $month."<br>".$date; $sql1="UPDATE $tbl_name SET name='$name[$i]', month='$month[$i]', date='$date[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } } if($result1){ header("location:update2.php"); } ?> <?php $result = mysql_query("SELECT * FROM test_mysql") or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Name</th> <th>Age</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['name']; echo "</td><td>"; echo $row['lastname']; echo "</td></tr>"; } echo "</table>"; ?> What it does is this, it gets the row from the database, echos it and I am able to edit it and then save. The thing is, it works with the updating but im trying to combine 2 variables into 1 and update that one also. Im trying to combine month and date textboxes into one variable in the database that is name. It for some reason updates the column name to A instead of what I had in the 2 fields month and date. Here is where I combine both fields into one. $name = $month."<br>".$date; And here is where $month and $date is set. $month = $_POST['month']; $date = $_POST['date']; Here is a picture. I am trying to create a simple calendar where a user can add/remove their availability date. This part works so far. Now what I am having trouble with is highlighting the already selected dates that are fetched from a MySQL database.
Here's my code. // HTML <div id="datetimepicker1"></div> // Jquery/Ajax <script> $(document).ready(function () { $('#datetimepicker1').datepicker({ dateFormat: "yy-mm-dd", multidate: true, onSelect: function () { var getDate = $("#datetimepicker1").val(); $.ajax({ type: "POST", //or GET. Whichever floats your boat. url: "snippets/adapter-set.php", data: { date: getDate }, success: function(data) { // alert(data); }, error: function() { alert("Error."); } }); } }); }); </script> // PHP // adapter-set.php $post_date = $_POST['date']; $find_query = $db->prepare("SELECT user_id FROM user_dates WHERE date_available = :date_available"); $find_query->bindParam(':date_available', $post_date); $find_query->execute(); $result_find = $find_query->fetchAll(PDO::FETCH_ASSOC); if(count($result_find) > 0) { foreach($result_find as $row) { $user_id = $row['user_id']; } $delete_query = $db->prepare("DELETE FROM user_dates WHERE user_id = :user_id"); $delete_query->bindParam(':user_id', $user_id); $delete_query->execute(); $result_delete = $delete_query->execute(); if($result_delete == false) { echo 'delete false'; } else { echo 'delete success'; } } else { $insert_query = $db->prepare("INSERT INTO user_dates(date_available) VALUES(:date_available)"); $insert_query->bindParam(':date_available', $post_date); $result_insert = $insert_query->execute(); if($result_insert == false) { echo 'insert false'; } else { echo 'insert success'; } }
The above code works fine. It inserts and deletes a date row in MySQL database table based on a click. Now what I would like to do is to highlight all the "available" dates that are already inserted into the database; so that the user knows which dates he has already selected. This is my code for that. But it doesn't seem to be working. No errors. It's just not highlighting the inserted dates. Can you tell me what I'm doing wrong? // JQUERY <script> $(document).ready(function() { $.post('snippets/adapter-fetch.php', {}, function(data){ $("#datetimepicker1").datepicker({ datesEnabled : data.datesEnabled }); }, 'json'); }); </script> // PHP // adapter-fetch.php $global_user_id = 5; $find_query = $db->prepare("SELECT date_available FROM user_dates WHERE user_id = :user_id"); $find_query->bindParam(':user_id', $global_user_id); $find_query->execute(); $result_find = $find_query->fetchAll(PDO::FETCH_ASSOC); if(count($result_find) > 0) { foreach($result_find as $row) { $date_available = $row['date_available']; echo $date_available; } } else { echo 'not dates available'; }
Hello, So I have a weird error where if I fetch more than a certain number of rows from a mysql table, it triggers a 500 Internal Server Error. I am using Apache Web Server (through GoDaddy) and the offending code is below: Code: [Select] set_time_limit(0); $this->Connect(); $Output = array(); $search = "SELECT * FROM <table> WHERE user_id = ?"; if($Statement = $this->MySQLi->prepare($search)){ $Statement->bind_param("i", $UserId); $Statement->execute(); $Statement->bind_result(<result variables>); $count = 0; while($Statement->fetch() && $count++ < 70){ ChromePhp::log(<result variables>); } $Statement->close(); } $this->Disconnect(); ChromePhp::log is a way of dumping things to the Javascript Console in your browser from within a PHP script just as a heads-up. So when I set the stop number as 70, everything is fine. If I try to fetch more than that it triggers a 500 internal server error on Apache Server port 443. I have looked through the error logs and can't figure out the cause but this is almost certainly a server configuration issue? I'd appreciate any feedback, especially anyone familiar with GoDaddy's hosting services Thanks This is really blowing my mind... the code is as follows the table, the column names everything is right and contains information but the variables aren't being filled... I don't get it... Anyone have any ideas or options I might not have thought of for debugging? $item_name = ("SELECT * FROM items WHERE id = '$grab_item'"); $que_items = mysql_query($item_name); while ($item_todel = mysql_fetch_array($que_items, MYSQL_BOTH)) { $name = $item_todel['name']; $desc = $item_todel['desc']; } echo ($name . "<br>" . $desc . "<br>" . $grab_item); the only variable thats set is grab_item, fetch_array throws no errors, if I plug in the query straight to the sql I get all the proper information... It's just not filling the two variables....
Hi,
<?php $connect = mysqli_connect('localhost', 'root', '1234', 'contact'); $query ='SELECT * FROM tbl_employee ORDER BY ID DESC'; $result = mysqli_query($connect, $query); ?>
<table id="employee_data" class="table table-striped table-bordered"> <thead> <tr> <td>Name</td> <td>Location</td> <td>Department</td> <td>Phone Number</td> </tr> </thead> <?php while($row = mysqli_fetch_array($result)) { echo ' <tr> <td>'.$row["name"].'</td> <td>'.$row["location"].'</td> <td>'.$row["department"].'</td> <td>'.$row['phone_number'].'</td> </tr> '; } ?> </table>
|