I'm trying to SELECT all rows as specified in the query below, but I think the issue is related to mysql_fetch_row, because in MYSQL Workbench the MYSQL query (yes I know I should be using PDO) shows all desired rows.
$result = mysql_query("SELECT zebra_id FROM zebra WHERE user_id = '$id' AND pref = '1'")
or die(mysql_error());
$array = mysql_fetch_row($result);
echo json_encode($array);
JSON Output (only showing 1 row):
["986"]
I tried changing to mysql_fetch_array, but that doesn't output all rows in the JSON encoded $array.
mysql_fetch_row() only fetchs one row as the name implies. So your error is obvious.
mysql_fetch_array() won't work unless you iterrate through the entire result set*. You just don't call it once and expect to get the entire result set in an array.
$result = mysql_query("SELECT zebra_id FROM zebra WHERE user_id = '$id' AND pref = '1'")
or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
echo json_encode($array);
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
You have to do this in a loop. See this:
$result = mysql_query("SELECT zebra_id FROM zebra WHERE user_id = '$id' AND pref = '1'")
or die(mysql_error());
$array = array();
while($row = mysql_fetch_row($result)){
$array[] = $row;
}
echo json_encode($array);
And also I have to say that, do not use mysql_* functions anymore. These are deprecated officially. You have to use mysqli_* or PDO instead.
Related
I am trying to get the last id which I entered to my database. Here is the code I use:
$test_query = "SELECT * FROM table ORDER BY id DESC LIMIT 1";
if ( mysql_query($test_query) ) {
echo 'OK!';
$results = mysql_fetch_array($test_query);
echo $results['id'];
print_r ($results);
}
The only output I have is the 'OK!'.
What do i do wrong?
You need to use the output of mysql_query in mysql_fetch_array.
$res = mysql_query($test_query);
if ($res === false) {
throw new Exception("query failed");
}
$row = mysql_fetch_array($res);
echo $row["id"];
Keep in mind that this reads only one row. If you want more use the while loop construction you can find here: http://php.net/mysql_fetch_array
If you just did an INSERT query use mysql_insert_id() to fetch the id. This is a feature of MySQL. This works in conjunction with the AUTO_INCREMENT option.
Also, if this is a new site you're building use mysqli_* functions instead of mysql_*. The latter is deprecated.
You can use this if you have an auto-increment field in the table:
SELECT LAST_INSERT_ID();
Just for now I am trying to output the value of the highest j_id row.
$result = mysql_query("SELECT max(`j_id` +1) FROM `journey`");
I'm also adding that 1 to j_id purposely.
It works fine within the phpmyadmin panel. But when I try to use it within any php script I get back resource id. I know that mysql_query() does not return an array, I've tried mysql_fetch_array($result) but that doesn't work.
Any help would be great?
$result = mysql_query("SELECT max(j_id +1) FROM journey");
$row = mysql_fetch_row($result);
$max_id_plus_one = $row[0];
$result is not the value you want - it contains it.
Note that the mysql extension is deprecated - you should move to mysqli or DBO.
Try this:
$result = mysql_query("SELECT max(j_id +1) as max_id FROM journey");
$row = mysql_fetch_assoc($result);
$max_id_plus_one = $row['max_id'];
you may try this
$result = mysql_query("SELECT max(j_id +1) as mid FROM journey");
$row = mysql_fetch_row($result);
$max_id_plus_one = $row['mid'];
See the Numeric Arrays AND Associative Arrays portion.
I think,Your Confusion will be clear...
mysql_fetch_array = Associative Arrays
mysql_fetch_row = Numeric Arrays
If you use mysql_fetch_array then see the column name from phpmyadmin, and get the data from array using that column name.
If you use mysql_fetch_row then get the data from $row[0].
I am trying to combine all the results as i go through the loop
$result = ....;
$finalresult = 0;
while($row = mysql_fetch_assoc($result))
{
$clubID = $row['clubID'];
$finalresult = mysql_query('"'.$finalresult.'" UNION ALL SELECT * FROM events WHERE clubID = "'.$clubID.'"');
}
However, this isn't working. Is there another way to do this?
This should do the trick:
$clubIDs = array();
while($row = mysql_fetch_assoc($result)) {
$clubIDs[] = $row['clubID'];
}
$finalresult = mysql_query("SELECT * FROM events WHERE clubID IN (" . implode(',', $clubIDs) . ")");
It's generating a list of clubIDs from your previous query, and putting them in an array. Then, it's using that array to generate an IN clause, and retrieving all the data in one go.
A couple of caveats - this will fail if there are no valid club IDs, so you'll want to add some error checking.
Secondly, mysql_* is deprecated in PHP 5.5, so you should look at using mysqli_* or PDO instead - they're both much more secure.
I am trying to get pagination working on my website. Everything should be working but this only displays one product, however that should be 8 products. Anybody know what's wrong?
Thanks in advance.
else {
$query = "SELECT COUNT(*) FROM products";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_fetch_row($result);
$pages = new Paginator;
$pages->items_total = $num_rows[0];
$pages->mid_range = 9;
$pages->paginate();
$query = "SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_row($result);
{
echo '<div style="margin-bottom:10px;display:inline-block;background-color:#E3E3E3;width:190px;height:200px;"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row[4]).'"><br><div align="center"><b>'.htmlspecialchars($row[1]).'</b><br><h6>€'.htmlspecialchars($row[3]).'</h6></div></div>';
};
echo ' ';
echo '<br><br><div style="margin-left:330px;">';
echo $pages->display_pages();
echo '</div>';
}
?>
mysql_fetch_row() only fetches one row at a time. You need to call it repeatedly to display all rows, like this:
while ($row = mysql_fetch_row($result)) {
// handle single row
}
I suggest you consider using mysql_fetch_array() instead. Then you will not have to rely on the order of the columns anymore and your code becomes more legible:
$row[0] becomes $row["serial"] etc.
Try reading the articles that #Kush linked. See here for a PHP-specific discussion.
You do not seem to be using mysql_fetch_row() properly
The following code is vulnerable to SQL injection because $page->limit does not appear to be sanitized
$query = "SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit";
Stop using mysql_* functions as they're deprecated. Use PHP database objects (PDO) instead because PDO allows parameter binding which protects you from sql injection.
You can read up on using PDO here
because of this line
$row = mysql_fetch_row($result);
should be
while($row = mysql_fetch_row($result)){...
Change the line
$row = mysql_fetch_row($result);
to
while ($row = mysql_fetch_row($result))
How do I show the results for $wordavg in php. I have done the query in SQL on database after taking out variables so I believe the query is correct but don't know how to show the results of the search in php.
$usertable = 'words';
$yourfield = 'wordname';
$query = "SELECT AVG(CHAR_LENGTH( wordname)) AS $wordavg FROM $usertable WHERE $yourfield LIKE '"."$current_letter"."%' ";
$result = mysql_query($query);
First, you should be using mysqli instead. Back to your question, usually you can iterate over a result with a loop as follows:
while ($row = mysql_fetch_assoc($result)) {
echo $row['field'];
}
More info and examples in the PHP mysql_query doc.
Since you only have one row of data to return, you don't need the loop part. You can simply use
$row = mysql_fetch_assoc($result);
$wordavg = $row['wordavg'];
You shouldn't have the $ in wordavg in your query. It should be just ...AS wordavg FROM...