I have this code for the select and printing values.
mysql_numn_rows returns null :/
<?php
mysql_connect('localhost','root','');
mysql_select_db("db2517");
$username = "Cristoforo";
$query = mysql_query("SELECT * users WHERE username='$username' ");
$numberOfRows = mysql_num_rows($query);
echo "num: $numberOfRows";
?>
First I would like to suggest you user PDO or MYQLI
In your query you have missed the from so replace your query:
From
$query = mysql_query("SELECT * users WHERE username='$username' ");
To
$query = mysql_query("SELECT * from users WHERE username='$username' ");
Better use mysqli or pdo instead of mysql.here you missed from in your query so your Query should be
$query = mysql_query("SELECT * from users WHERE username='$username' ");
Related
I wrote the following query:
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT * FROM students_in_session WHERE Username = '$email')";
$res = mysqli_query($conn,$query1);
$query2 ="SELECT * FROM students_in_session WHERE Username='$email'";
$res2 = mysqli_query($conn,$query2);
if (!$res) {
die(mysqli_error($conn));
}else{
while ($row = mysqli_fetch_array($res)) {
print_r($row);
$course = $row['Degree'];
$date = $row['Date'];
$hour = $row['Hour'];
$room = $row['Room'];
}
}
if(!$res2){
die(mysqli_error($conn));
}else{
while ($row = mysqli_fetch_array($res2)) {
print_r($row);
$prof = $row['Professor'];
$assis = $row['Assistent'];
}
}
return "\n\nDegree: ".$course."\n"."Date: ".$date."\n"."Hour: ".$hour."\n"."Room: ".$room."\n"."Prof: ".$prof."\n"."Assistent: ".$assis;
}
Currently using phpmyadmin and testing the query returns the expected result,but using the query in the code the variables are all empty.
These are DB's Table:
session
|Id_Session|Date|Hour|Room|Degree|
students_in_session
|Id_Session|Code|Name|Surname|Username|Professor|Assistent|
In query1 you are just try to compare Id_session with the whole table you need to compere with the session id only.
Just replace this
$query1 = "SELECT * FROM session WHERE Id_session IN
(SELECT * FROM students_in_session WHERE Username = '$email')";
with
$query1 = "SELECT * FROM session WHERE Id_session IN
(SELECT Id_Session FROM students_in_session WHERE Username = '$email')";
I thing it will work for you.
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT * FROM students_in_session WHERE Username = '$email')";
In this query you must need to select only one column like
$query1 = "SELECT * FROM session WHERE Id_session IN (SELECT Id_session FROM students_in_session WHERE Username = '$email')";
Check it.
when you use a IN over a nested query, the nested query can return only one column:
SELECT * FROM session WHERE Id_session IN
(SELECT Id_Session FROM students_in_session WHERE Username = '$email')
However a nested query is not the best approach in your case, because MySQL will have to execute 2 queries. You would better do an INNER JOIN :
SELECT S.*
FROM session S
INNER JOIN students_in_session SS ON SS.Id_Session=S.Id_Session
WHERE SS.Username = '$email'
How do I get the outcome of
SELECT COUNT(user) FROM game_paths WHERE user = '$user'
to a PHP variable
I tried
mysql_num_rows
but it returns nothing.
You should use mysql_result and get the first column of the result. Like this:
$result = mysql_query("SELECT COUNT(user) FROM game_paths WHERE user='$user'");
$count = mysql_result($result, 0);
You can also alias the column like this:
$result = mysql_query("SELECT COUNT(user) AS total FROM game_paths WHERE user='$user'");
$data = mysql_fetch_assoc($result);
$count = $data['total'];
Which might be better if you're going to select several columns at the same time, and also for readability.
Try like this. you need to use mysql_fetch_assoc or mysql_fetch_array functions
$result = mysql_query(" SELECT COUNT(user) as total FROM game_paths WHERE user='$user' ");
$row = mysql_fetch_assoc($result);
echo $row['total'];
Or
$result = mysql_query(" SELECT COUNT(user) FROM game_paths WHERE user='$user' ");
$row = mysql_fetch_array($result);
echo $row[0];
Docs Link: http://us2.php.net/mysql_fetch_array
http://www.w3schools.com/php/func_mysql_fetch_array.asp
Note: mysql_* function are deprecated try to use mysqli or PDO
You can use the following code:
$result = mysql_query(" SELECT COUNT(user) FROM game_paths WHERE user='$user' ");
$row = mysql_fetch_array($result);
$count= $row[0];
or
$result = mysql_query("SELECT * FROM game_paths WHERE user='$user'");
$count=mysql_num_rows($result);
This will return the number of rows satisying the condition.
Hey friend Try this code, ithink this will solve your problem
<?php
$con=mysql_connect('hostname','DBusername','paassword');
mysql_select_db('Db_name',$conn);
$query="SELECT COUNT(user) as total FROM game_paths WHERE user='$user'";
$result=mysql_query($query,$con);
$row=mysql_fetch_array($result);
echo $row['total'];
?>
$result = mysqli_query("SELECT COUNT(user) AS user_count FROM game_paths WHERE user='$user'");
$result_array = mysql_fetch_assoc($result);
$user_count=$result_array['user_count'];
Please use mysqli_ instead of mysql_ as its deprecated in the new version
i have a basic doubt.
i have this example:
$sql = mysql_query("SELECT * FROM admin where id = username='$username' and password = '$password' LIMIT 1");
Its possible to see what this sql command is doing in the browser? To see if it is correct..
Thanks.
$a = "SELECT * FROM admin where id = username='$username' and password = '$password' LIMIT 1";
echo $a;
$sql = mysql_query($a); // use mysqli instead
if you will print the $sql it will print #resurce #4
you need to do:
$q = "SELECT * FROM admin where id = username='$username' and password = '$password' LIMIT 1";
print_r($q);
and you see it in the network tab on the console or in the browser if you print your request
<?=$this->db->count_all_results('users')?>
Hi, I am stuck on something. How can run a query like the below count and echo all of the returned rows? I am currently using the above statement but I need this improvement.
<?php $aa = $this->db->query("SELECT * FROM users WHERE itemNumber = 1");
Use num_rows() method
<?php
$aa = $this->db->query("SELECT * FROM users WHERE itemNumber = 1");
echo $aa->num_rows();
?>
$this->db->query("SELECT u.*, COUNT(u_all.*) FROM users u, users u_all WHERE u.itemNumber = 1");
$numRows = $this->db->query("SELECT count(*) FROM users WHERE itemNumber = 1");
I have this very simple function:
function getCatName($id){
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";
$res = mysql_query ($sql) or die (mysql_error ());
$row = mysql_fetch_assoc ($res);
$name = $row["Name"];
return $name;
}
So with this function I should be able to get the category name, but it doesn't work with the parameter. If I put 8 or 9, the categoryname is displayed correctly.
The id is also passed on like it should, when I print it out, it shows 8 or 9.
I know the solution is quite simple, I just don't see it.
To fix remove the quotes and check the column name for case id or ID. Since the query string is in double quotes you don't have to use the . join
$sql = "SELECT * FROM biznet_category WHERE ID = $id";
You can use curly brackets which I find easier to read
$sql = "SELECT * FROM biznet_category WHERE ID = {$id}";
If you were querying a string rather than an integer you can simply do
$sql = "SELECT * FROM biznet_category WHERE ID = '{$id}'";
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";
To
$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;
Try this
$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;
Is the column name ID spelt correctly?