I have this mysql query in my mysql include file sql.php :
$resultid = array();
$sqlstring9 = "SELECT * FROM auswahl_time WHERE IsActive = 1 ORDER BY Time_PKey ASC";
$resultid[9] = mysql_query($sqlstring9);
$resultid10 = mysql_query($sqlstring9);
On my index page :
echo mysql_num_rows($resultid[9]);
echo mysql_num_rows($resultid10);
returns only the result for $resultid10 not $resultid[9]. I got this to work before and im not sure what changed since then. What are the requirements for this to work? Do i need to initialize the array for resultid in every script?
I found the problem, turns out i had another unrelated query called $resultid. After renaming it it worked.
Try this
$sqlstring9 = "SELECT * FROM auswahl_time WHERE IsActive = 1 ORDER BY Time_PKey ASC";
$resultid = mysql_fetch_assoc(mysql_query($sqlstring9));
echo $resultid[9];
echo $resultid[10];
Related
I am a novice when it comes to PHP but I don't understand if my syntax is wrong in this statement, or how would I grab an int from my MySQL server.
I know that my server credentials are working fine. How would I fix this statement to give me a returned integer of the number of reviews in the userinfo table?
$numberofpreviousreviews = mysql_query("SELECT `number_of_reviews` FROM `userinfo`") or die(mysql_error()); //Check to see how many reviews user has previously created
$amountofreviews = $numberofpreviousreviews + 1;
$query2 = mysql_query("ALTER TABLE userinfo ADD `amountofreviews` VARCHAR(10000)") or die(mysql_error()); //Make another column in database for the new review
You need to fetch your results after you run your query. There are several ways to do this but using mysql_fetch_assoc() will work for you.
$numberofpreviousreviews = mysql_query("SELECT `number_of_reviews` FROM `userinfo`") or die(mysql_error()); //Check to see how many reviews user has previously created
$row = mysql_fetch_assoc($numberofpreviousreviews);
$amountofreviews = $row['number_of_reviews'] + 1;
FYI, you shouldn't be using mysql_* functions anymore. They are deprecated and going away. You should use mysqli or PDO.
Assume you have a table userinfo which has the following structure and data :
Scenario #1 :
If you want to retrieve the all number_of_reviews, then do like this,
$query = "SELECT `number_of_reviews` FROM `userinfo`";
$result = mysqli_query($db,$query);
while ($row = mysqli_fetch_assoc($result)) {
echo "Number of reviews : " . $row['number_of_reviews'] . "<br/>";
}
It will give you,
Number of reviews : 20
Number of reviews : 40
Since, the result has many rows, it will display like above.
Scenario #2:
If you want to retrieve only the specific number_of_reviews for some user id (which is unique). I take id as 1 as a example here. Then do like,
$query2 = "SELECT `number_of_reviews` FROM `userinfo` WHERE `id` = 1";
$result2 = mysqli_query($db,$query2);
while ($row2 = mysqli_fetch_assoc($result2)) {
echo $row2['number_of_reviews'] . "<br/>";
}
This will print,
20.
Because, number_of_reviews is 20 for id 1.
while executing the below query i'm not getting sucess value
$name=qwe;
$result = mysql_query("SELECT *FROM USER WHERE name = $name");
Instead of above query if i put the below, I can able to get the appropriate ans:
$result = mysql_query("SELECT *FROM USER WHERE name = 'qwe'");
Can anyone give a solution for my first query??
Close the variable in quote and concatenate the string into query
$name= "qwe";
$result = mysql_query("SELECT *FROM USER WHERE name = '".$name."'");
one more note
please use mysqli for security purposes. Thanks
Use the following:
$result = mysql_query("SELECT *FROM USER WHERE name = '".$name."'");
P.S. I have not tested the above code but it should work.
select * from table_name where col_name = '".$variable."'
Directly use :
$name = 'qwe';
$result = mysql_query("SELECT * FROM USER WHERE name = '$name'");
And on more advise, you should never use mysql_* functions now as they have been deprecated in newer versions of PHP and will produce the notices.
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'];
}
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
Im trying to run a MySQL query, but for some reason its not working.
This is the query im running:
SELECT * FROM applications WHERE status = 0
And my database looks like this:
http://gyazo.com/cc0bfa109e73a771d99a22b5051ee2de
However, num_rows() returns 0 rows...
No errors are displayed...
Try below code hope it will help you because i have tested in localhost.
$sql = "SELECT * FROM applications WHERE status = 0";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row['mcUser'];
}
IF its not work's check your table name again.
replace the query with single quotes between 0
$sql = "SELECT * FROM applications WHERE status = '0'";