MySQLi Query will not execute - php

I am playing around with this trying to pull some articles from a database.
Here is the code i have so far: ($host etc are defined in config)
include ("admin/config.php");
$mysqli = mysqli_connect($host, $user , $pass , $database);
$query = "SELECT * FROM articles ORDER BY date LIMIT 5";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result))
{
Some code to display results
}
This is the error that gets generated but i just cant see what i am doing wrong. Any help will be much appreciated.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /website....

there is error in your query.
to get it always run all your queries this way
$result = mysqli_query($mysqli,$query) or trigger_error($mysqli->error."[$query]");

Related

Using mysqli_query with mysql_query arguments

I'm migrating from PHP 5 to PHP 7, and I made a grep to get all mysql_query to change them to mysqli_query. The problem is, apparently the parametres changed using the procedural style.
mysql_query($query, $link_identifier)
mysqli_query($link_identifier, $query)
Even tho using the object oriented style they're still the same parametres.
Question is, will it work if I leave the parameters of mysqli_query as $query, $link thinking that the function is smart enough to detect which is which or I need to change them all to match the right parameters ?
Order of arguments isn't interchangeable.
Php.net mysqli_query.
Php.net mysql_query.
I also tested it.mysql_query
$link = mysql_connect("localhost","login","pass");
$db_selected = mysql_select_db('db', $link);
$sql = "SELECT * FROM table LIMIT 5";
$result = mysql_query($sql, $link);
while($row = mysql_fetch_array($result)) {
echo $row['col_name'];
}
mysql_close($link);
mysqli_query with with wrong order of arguments will produce: Warning: mysqli_query() expects parameter 1 to be mysqli, string given code here:
$link = mysqli_connect("localhost", "login", "pass", "db");
//arguments in wrong order
if ($result = mysqli_query("SELECT * FROM table LIMIT 5", $link)) {
while($row = mysqli_fetch_array($result)) {
echo $row['col_name'];
}
}
//produce
//Warning: mysqli_query() expects parameter 1 to be mysqli, string given in
mysqli_close($link);

How do I store the results of an SQL call in a php array

I have been trying to create a sign up form for a website and I keep struggling with trying to check if a user's username had been used by someone else who signed up for the website in the past. I am using a Mysql database to store information and am trying to access past usernames through it using php. This specific part of my code keeps returning an error.
$query = "SELECT username FROM users";
$records = mysql_query("SELECT username FROM users");
$result = mysql_query($records);
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row['username'];
}
The error messages I am receiving are:
Warning: mysql_query() expects parameter 1 to be string, resource given in ...
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in...
thank you so much!
The problem is you are doing mysql_query twice one on the $records variable and then on resultlike so:
$records = mysql_query("SELECT username FROM users");
$result = mysql_query($records);
What you need to do is this:
$records = "SELECT username FROM users";
$result = mysql_query($records);
I hope the answer by Jack Smith was helpful. I'll just add that try to use mysqli_query as mysql is deprecated and may be removed in a newer version of php. You would also need to use mysqli_connect('host','usrname','password','database') instead of mysql_connect.

ERROR: mysqli_num_rows() expects parameter 1 to be mysqli_result

Im very new to php and the mysql side of things and i was hoping someone could help me out, My code is returning the error: mysqli_num_rows() expects parameter 1 to be mysqli_result
Here is my code
$username = $_POST['username'];
$password = $_POST['password'];
if(isset($username) && isset($password)) {
$connect = mysqli_connect("localhost","root","") or die("Couldnt connect!");
mysqli_select_db($connect,"phplogin") or die("Couldnt find DB.");
$query = "SELECT * FROM users WHERE username={$username}";
$numrows = mysqli_num_rows($query);
echo $numrows;
If someone could help me out and tell me what i done wrong and how i could echo out the number of rows i would glady appreciate it.
Function mysqli_num_rows expects result, you gave it string.
Before num_rows you have to call $result = mysqli_query($db_connect, $query). Then you can count records using mysqli_num_rows($result);.
You have to actually run the query first before you can see how many rows you have:
$query = "SELECT * FROM users WHERE username={$username}";
// Run the query
$result = mysqli_query($connect, $query);
$numrows = 0;
if(!$result)
{
$numrows = mysqli_num_rows($result);
}
Remember to look up on the php.net website if you get stuck, it's a goldmine of help: http://www.php.net//manual/en/mysqli-result.num-rows.php

MySQL Query using escaped $_SESSION var returns boolean?

I'm having a little bit of trouble. I need to return a set of data in a table.
Quickly here is the table structure -
DB ->
users -> user_id , user_name , user_pass , user_email ;
urls -> user_id , url , title ;
On the page I have the currently logged in users, user_id inside a $_SESSION variable - SESSION 'user_id'. I need to return all the (url)s inside the urls table that have the same matching user_id as the one set in the SESSION variable. Here's the code I have and it almost works, but says when I'm trying to fetch an array it is getting a boolean from $results. Any help will be greatly appreciated! The last line print_r is there just to see what is being returned.
$mysqli = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sql = "SELECT * FROM url WHERE user_id = ";
$sql .= mysqli_real_escape_string($mysqli, $_SESSION['user_id']);
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
print_r($row);
Here is the error I am getting -
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\practice\include\feed.php on line 10
Line 10 is this line...
$row = mysqli_fetch_array($result, MYSQLI_NUM);
Thanks again!
try
$mysqli = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$escapedID = mysqli_real_escape_string($mysqli, $_SESSION['user_id']);
$sql = "SELECT * FROM url WHERE user_id = '".$escapedID."'";
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_array($result, MYSQLI_NUM);
print_r($row);
mysqli_query() returns FALSE on failure. See the documentation here. The query is might be failing because you've written "url" in the FROM clause; you have the table name listed as "urls" above.

Can't retrieve DB info

I need help figuring out why the following DB Query is not working. I know the DB connection is good. I also know the $referralname = $_SESSION['user_name']; is correctly rendering. It has to be something with my code.
I am getting the following errors. Maybe this will help to figure this out.
[12-Jun-2013 21:13:54 America/New_York] PHP Warning: mysql_query() expects parameter 1 to be string, object given in /x/x/public_html/americansolar/partner/classes/Referral.php on line 89
[12-Jun-2013 21:13:54 America/New_York] PHP Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /x/x/public_html/americansolar/partner/classes/Referral.php on line 90
P.S. I am not sure if the while statement is necessary or not since it will always only return one result???
My Code:
// creating a database connection
$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// if no connection errors (= working database connection)
if (!$this->db_connection->connect_errno) {
$referralname = $_SESSION['user_name'];
// get the referrer's id
$query_get_referral_id = $this->db_connection->query("SELECT * From users WHERE user_name = '".$referralname."';");
$result = MYSQL_QUERY($query_get_referral_id);
$numberOfRows = MYSQL_NUM_ROWS($result);
$i = 0;
while ($i<$numberOfRows)
{
$thisId = MYSQL_RESULT($result,$i,"user_id");
$i++;
}
}
My Solution:
$query_get_referral_id = $this->db_connection->query("SELECT * From users WHERE user_name = '".$referralname."';");
while($row = mysqli_fetch_array($query_get_referral_id))
{
$thisId = $row['user_id'];
}
Youre mixing mysqli and mysql... they are two completely different and incompatible interfaces. Secondly, your $query_get_referral_id is not an id value... it is a mysqli_result object. You need to then extract the value from that object.
And lastly... DONT use mysql... stick with mysqli, or use PDO
Also you should use a prepared statement for this:
$stmt = $this->db_connection->query("SELECT user_id From users WHERE user_name = ?");
$stmt->bind_param('s', $referralname);
$stmt->execute();
if($stmt->num_rows) {
$stmt->bind_result($userId);
while($stmt->fetch()) {
// do something with $userId...
// each iteration of this loop is a
// row of the result set, it will automatically
// load the value of the user_id into $userId
}
}
I donot think you should query like
$query_get_referral_id = $this->db_connection->query("SELECT * From users WHERE user_name = '".$referralname."';");
$result = MYSQL_QUERY($query_get_referral_id);
Well, you should go
$result = $this->db_connection->query("SELECT * From users WHERE user_name = '".$referralname."';");

Categories