Use of mysql_query for variable declaration - php

In the following PHP code, s_at is a column that includes integer values in database. I need to declare one of that value where index is 1, to $s_at. But won't happen in this way. I'm glad if anyone could help.
$s_at = mysql_query("Select('s_at') FROM `time_holder` WHERE `index` = 1");

quite easy:
$mi_resource = mysql_query("Select `s_at` FROM `time_holder` WHERE `index` = 1");
// .... error checking left out
$records = mysql_fetch_assoc($mi_resource);
// .... error checking left out
$s_at = $records[$record_num]['s_at']; // probably #0 if only one record.
...
see mysql_query

Change your code to this and it will work for sure :
$result = mysql_query("Select `s_at` FROM `time_holder` WHERE `index` = 1");
$row = mysql_fetch_array($result);
$s_at = $row['s_at'];

Related

How do you select multiple rows from a mysql table?

I have a php script that is supposed to get multiple rows from a table and then wrap each row as an array into another array.
$comQy = "SELECT * FROM comments WHERE user = '$user' ORDER BY DESC;";
$comSt = $db->prepare($revQy);
$comRes = $comSt->execute();
$coms = $comSt->fetchAll();
Later in the page, I try to echo one of the elements of the array and then it doesn't work but doesn't return an error.
<div id="comUser">
<?php echo $coms[0]['user'] ?>
</div>
I appreciate all help and I am sorry if I have made a fairly simple mistake in the php script.
This could be the problem of
missing the field name for the ORDER BY clause in the SQL query
missing the variable declaration $revQy
missing the object variable declaration $revSt
$comSt = $db->prepare($revQy);
$comRes = $revSt->execute();
Enabling error reporting is a good practice during development. Add these lines of code at the top of your script.
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
$comQy = "SELECT * FROM comments WHERE user = :user ORDER BY 1 DESC;";
$comSt = $db->prepare($comQy);
$comRes = $comSt->execute(array( 'user' => $user ));
$comSt->setFetchMode(PDO::FETCH_ASSOC);
$coms = $comSt->fetchAll();
try to use msqli procedural method (im just a newbie too)
$sql = 'SELECT * FROM comments WHERE user = "'.$user.'" ORDER BY fieldname DESC';
$result = mysqli_query($db_connection, $sql);
while($row = mysqli_fetch_assoc($result)){
echo $row['user'];
}

How to display field from MySQL?

I'm trying to display a field from my MySQL database. It's in the table tblproducts in the row with the id is set to 1. And under the column qty.
This is the code I'm using:
<?php
mysql_connect("localhost","username","password");
mysql_select_db("database_name");
$available = "SELECT qty FROM tblproducts WHERE id = 1";
$result = mysql_query($available);
echo $result;
?>
However, I keep getting this message: Resource id #2
I've done a bit of research and seen where other people are having similar problems but most of them are trying to display their data in an HTML table whereas I just need the data from 'qty' to display. And of course I'm definitely not a MySQL guru.
Can anyone help me out with this please?
Try changing this:
$result = mysql_query($available);
To this:
$result = mysql_result(mysql_query($available), 0);
Let's start from the start. (I'll assume you have the connection set)
Form the query
$query = "SELECT `qty`
FROM `tblproducts`
WHERE `id` = 1";
Execute the query
$run = mysql_query($query);
Now, put the result in an assoc array
$r = mysql_fetch_array($run);
See the contents of the array
echo $r['qty'];
It's also advised that you move up from mysql to either mysqli, or PDO. PDO is preferred as you're not bound to the MySQL database model.
Try this:
Here you need to generate associative array and then get the resulting row.
$query = "SELECT `qty` FROM `tblproducts` WHERE `id` = 1";
$run = mysql_query($query);
$r = mysql_fetch_array($run);
echo $r['qty'];
-
Thanks

how to get a particular field from a database via php

I am trying to get four different values from my database. The session variable username and usernameto are working, but I want to get 4 different values -- two each from username and usernameto:
<?php
session_start(); // startsession
$Username=$_SESSION['UserID'];
$Usernameto= $_SESSION['UserTO'];
$db = mysql_connect("at-web2.xxxxxx", "yyyyy", "xxxxxxx");
mysql_select_db("db_xxxxxx",$db);
$result1 = mysql_query("SELECT user_lon and user_lat FROM table1 WHERE id = '$Usernameto'");
$result2 = mysql_query("SELECT user_lon and user_lat FROM table1 WHERE id = '$Username'");
$myrow1 = mysql_fetch_row($result1);
$myrow2 = mysql_fetch_row($result2);
while($myrow1)
{
$_Mylon=$myrow1[0];
$_Mylat=$myrow1[1];
}
while($myrow2)
{
$_Mylon2=$myrow2[0];
$_Mylat2=$myrow2[1];
}
?>
Edit - just realized that you didn't tell us what wasn't working about the code you provided. Are you getting an error message or are you not getting the correct data back? You still should fix your query, but we'll need some more information to know what's wrong.
Your query statements shouldn't have "and" between the select parameters, so it should be:
Edit 2 - I just noticed that you had a while loop that you don't need, try this:
$result1 = mysql_query("SELECT user_lon, user_lat FROM table1 WHERE id = '$Usernameto'");
$result2 = mysql_query("SELECT user_lon, user_lat FROM table1 WHERE id = '$Username'");
$myrow1 = mysql_fetch_row($result1);
$myrow2 = mysql_fetch_row($result2);
if (isset($myrow1)) {
$_Mylon=$myrow1[0];
$_Mylat=$myrow1[1];
}
if (isset($myrow2)) {
$_Mylon2=$myrow2[0];
$_Mylat2=$myrow2[1];
}
An example from the php manual echoing an html table
I don't know if you can derive what you need from this?
More specific: You can use:
$line = mysql_fetch_array($result, MYSQL_ASSOC);

Multiple Conditions in Where Clause, breaking with second condition

This is a very simple issue. I must just be doing something stupid:
This query echos out a row id number:
$query = "SELECT * FROM userpage WHERE uploaderrating = $rating";
$result = mysql_query($query);
$row1= mysql_fetch_array($result);
echo $row1[id];
When I add in an additional condition (even though the condition is DEFINITELY met in the SQL database the the echo does produce anything (i.e. the variable is empty). The failing code is:
$query = "SELECT * FROM userpage WHERE uploaderrating = $rating and reviewer = NULL";
$result = mysql_query($query);
$row1= mysql_fetch_array($result);
echo $row1[id];
You can't use
reviewer = NULL
You need to use
reviewer IS NULL
NULL is an undefined value, so it isn't equal to anything; so you need to use IS to look for it.
you should use
$query = "SELECT * FROM userpage WHERE uploaderrating = $rating and reviewer IS NULL";
$result = mysql_query($query);
$row1= mysql_fetch_array($result);
echo $row1[id];
I hope it works.
As far as I know null values are verified with IS
... where xxx IS null
... where xxx IS NOT null

While returning no results

I've got the following query in an existing script - but it's not always returning a value even though it should based off what's in the database. There are plenty of things in the database it SHOULD be grabbing - they are there.
Don't see anything wrong with it - but I barely do this anymore :) See anything?
$query = "SELECT id FROM xtags WHERE tag_id = '$tagid' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query = "SELECT * FROM xtable WHERE id = '$row[id]'";
$result = mysql_query($query) or die(mysql_error());
$row2 = mysql_fetch_assoc($result);
echo $row2[title];
}
$result is being used inside the loop and outside, try making a new variable inside and not reusing the outside one.
You're reusing the $result variable inside the loop which overwrites the value for use in the while condition. Use a different name for $query and $result inside the loop.
I don't know if is ok, but you are using $result twice, one before the "while" and another inside the "while".
I would personally split the string and the variable $row.
Why not use var_dump() to see $row and the other variables ???
I don't understand what you are trying to do actually, but try this:
$query = "SELECT id FROM xtags WHERE tag_id = '".$tagid."' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query2 = "SELECT * FROM xtable WHERE id = '".intval($row[id])."'";
$result2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
echo $row2[title];
}
Problem solved - did a join statement.

Categories