Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I try to get list after query by using php drop down list.
My code looks like this for now:
$con = mysql_connect($server,$user_name,$password,$database);
$db_found = mysql_select_db($database, $con);
if ($db_found) {
print "Database Found ";
}
else {
print "Database NOT Found ";
}
$result = mysql_query($con,$db_found,"SELECT
connection_log.user,count(*)
as 'connection_count', sum(TIMESTAMPDIFF(MINUTE,
connection_log.logondate,connection_log.logoffdate))
as `connection_time`
FROM connection_log
INNER JOIN users
ON connection_log.user=users.user WHERE users.groups
LIKE '%cpg_sirket_dyo%'
and connection_log.logondate>='2015-09-01 00:00:00'
and connection_log.logoffdate<'2015-10-01 00:00:00'
group by connection_log.user
order by connection_count desc");
while ($row = mysql_fetch_array($result)) {
echo $row['name']."<br />";
}
But when I run this php I get some error like shown below:
Database Found PHP Warning: mysql_query() expects at most 2 parameters, 3 >given in /var/www/html/test.php on line 22
PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, null >given in /var/www/html/test.php on line 23
Have you any idea?
mysql_query
mysql_query ( string $query [, resource $link_identifier = NULL ] )
Only need two parameter needed
1)Your query.
2)The MySQL connection
You just remove your database name inside your mysql_query also swap position of query and connection
$result = mysql_query("SELECT....",$con);
Use backtick in column and table name instead of quotes
Correct your qyery with
SELECT
connection_log.user,count(*)
as `connection_count`, sum(TIMESTAMPDIFF(MINUTE,
connection_log.logondate,connection_log.logoffdate))
as `connection_time`
FROM connection_log
INNER JOIN users
ON connection_log.user=users.user WHERE users.groups
LIKE '%cpg_sirket_dyo%'
and connection_log.logondate>='2015-09-01 00:00:00'
and connection_log.logoffdate<'2015-10-01 00:00:00'
group by connection_log.user
order by connection_count desc
Note:- mysql is deprecated instead use mysqli or PDO
Have a look at http://php.net/manual/en/function.mysql-query.php
There are only 2 parameters, the query and (optinal) the connection, so you have to use:
$result = mysql_query("SELECT ...", $con);
You need to remove this $db_found out of your function :
http://php.net/manual/en/function.mysql-query.php
$result = mysql_query($yourQuery,$con);
Also try using mysqli instead of mysql which is deprecated.
MySQL vs MySQLi when using PHP
Use below code,
$con = mysql_connect($server,$user_name,$password);
$db_found = mysql_select_db($database);
if ($db_found) {
print "Database Found ";
}
else {
print "Database NOT Found ";
}
$result = mysql_query("YOUR QUERY",$con);
while ($row = mysql_fetch_array($result)) {
echo $row['name']."<br />";
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been trying to code an API that when a button is pressed on a program is fetches and displays a random row from a mysql database in this format - Text:Text - My code so far doesn't seem to work? Have a look:
<?php
// Create connection
$con=mysqli_connect("example.com","user","pass","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysql_query("SELECT * FROM `data_submit` ORDER BY RAND() LIMIT 0,4;");
$row = mysql_fetch_array($result);
echo $row['name'];
mysqli_close($con);
?>
Please help!!! It's really annoying.
Thanks
You're mixing mysql_* and mysqli_* extensions. You can't use them interchangeably, you must use one or the other (since mysql_* is deprecated, as per my comment above, you should consider using MySQLi). You also must loop over the result, since you're returning an array:
<?php
// Create connection
$con=mysqli_connect("example.com","user","pass","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM `data_submit` ORDER BY RAND() LIMIT 0,4;");
while ($row = mysqli_fetch_array($result)) {
echo $row['name'];
}
mysqli_close($con);
?>
You start with a mysqli_connect but switch to the mysql_query (the non-improved version). You should stick to the mysqli-library: mysqli_query and mysqli_fetch_array
Sidenote your current query will be very slow on larger tables, here is a faster one.
SELECT *
FROM table
WHERE id >= ( SELECT FLOOR( MAX(id) * RAND()) FROM table )
ORDER BY id LIMIT 4
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this table in mysql database
number | name | friend
-------------------------------
1 |John | Kloey
2 |John | Tara
3 |Marco | Tia
4 |John | Clara
I need to get all the names in the friend column that corresponds to john in name column in an array or a string or anything that i can then receive in android?
How can i do this?
Here's my php code but it just returns the 1st name which is kloey only
$username = $_POST["username"];
$recordofusername = mysql_query("SELECT * FROM friends WHERE name='$username'");
$value1 = mysql_fetch_array($recordofusername);
$user_friendname = $value1['friend'];
$response["userfriends"] = $user_friendname;
echo json_encode($response);
Please don't use deprecated mysql_* extension.
Don't interpolate query strings leaving your code vulnerable to sql injections. Learn and use prepared statements with either mysqli_* or PDO
When you get the resultset which contains more then one row you should iterate over the resultset to fetch all the rows, which you didn't in your code. And that's why with your code you always get the only one name from the first row in the resultset.
Don't use SELECT *. Instead explicitly tell what columns you need to select. In your case you just need friend column.
That being said to give you an idea how your code might look like using mysqli
$name = $_POST['username'];
$friends = array();
$db = new mysqli('localhost', 'user', 'password', 'dbname');
if (!$db) {
die('Could not connect: ' . $db->connect_error); //TODO: better error handling
}
$sql = "SELECT friend FROM friends WHERE name = ?";
if ($stmt = $db->prepare($sql)) {
$stmt->bind_param('s', $name);
$stmt->execute();
$stmt->bind_result($friend);
while ($stmt->fetch()) { // you have to iterate over the resultset
$friends[] = $friend;
}
$stmt->close();
} else {
die('Prepare failed: ' . $db->error); //TODO: better error handling
}
$db->close();
$response['userfriends'] = $friends;
echo json_encode($response);
Sample output:
{"userfriends":["Kloey","Tara","Clara"]}
I have been on the site for some time now and I can't seem to get the idea that most of the similar questions are getting answers for this error:
Catchable fatal error: Object of class mysqli could not be converted to string
Saying it is an object. I am fairly new to PHP and it would really help if anyone could explain this to me. I am trying to retrieve data from my database and echo it in a table.
This is what I have done so far:
$dbcon=mysqli_connect("localhost","root","","technoage");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysql_query("SELECT * FROM items WHERE item_id = 1,$dbcon");
if(!$results)
{
die("Database query failed".mysql_error());
}
while($row = mysql_fetch_array($results))
{
echo $row['descreption']." ".$row['price']."<br/>";
}
You are connected with mysqli
but your are querying with mysql
$results = mysql_query("SELECT * FROM.....
rewrite your code with mysqli not mysql as you are mixing mysqli with mysql.
this
$results = mysql_query("SELECT * FROM items WHERE item_id = 1,$dbcon");
should be
$results = mysqli_query($dbcon, "SELECT * FROM items WHERE item_id = 1");
and this
while($row = mysql_fetch_array($results))
should be
while($row = mysqli_fetch_array($results))
This error is not caused by mixing mysql and mysqli. It's caused by inserting $dbcon into your SQL string itself, here:
$results = mysql_query("SELECT * FROM items WHERE item_id = 1,$dbcon");
// ^ here
The connection should be passed as the first argument with the query as the second. You're inserting it into the SQL string and passing it as a single argument, which means PHP tries to convert the object to a string.
Change to:
$results = mysqli_query($dbcon, "SELECT * FROM items WHERE item_id = 1");
As others point out, you need to change all mysql_ references to mysqli_ but this is your secondary problem. The immediate problem is the string issue and once that's fixed, you will encounter the mixing mysql problem.
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 1 year ago.
I am currently experiencing issues with the following script. Upon execution of the script, I do recieve the message "Connection was OK!" however, then I also receive the following messages:
Warning: mysql_query() expects parameter 2 to be resource, object
given in /opt/lampp/htdocs/worldofclucky.net/scripts/auth.php on line
11
Warning: mysql_fetch_array() expects parameter 1 to be resource, null
given in /opt/lampp/htdocs/worldofclucky.net/scripts/auth.php on line
12
Any idea what I am doing wrong? I am far from a PHP/MySQL expert, I wouldn't really even consider my self a novice... I did do some testing and the $username variable is sending from the previous page correctly and when typing SELECT * FROM forum.mybb_users WHERE username = 'x_clucky' LIMIT 1 into the MySQL client, it gives all of the information you would expect to get. The PHP code is as follows:
<?php
$username=$_POST["username"];
$hashed_password = md5($_POST['password']); /* For MyBB its $mybb->input['password'] */
$con=mysqli_connect("worldofclucky.net","clucky","CENSORED","forum");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else { echo "Connection was OK!\n";}
$query = mysql_query("SELECT * FROM mybb_users WHERE `username` = '$username' LIMIT 1",$con);
$row = mysql_fetch_array($query);
$encrypted_password = md5(md5($row['salt']).$hashed_password);
if($encrypted_password == $row['password']) {
echo "<script>alert('test');</script>";
}
mysqli_close($con);
?>
Thank you in advanced for your help
change mysql to mysqli and use below kind of query. You can't use mysql and mysqli altogether.
$query = mysqli_query($con, "SELECT * FROM mybb_users WHERE `username` = '$username' LIMIT 1");
$row = mysqli_fetch_array($query);
From a quick look it seems like you are using mysqli functions to connect and then mysql functions to make the actual query. mysql_* functions are now deprecated.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in php
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
Im working with two MYSQL databases trying to gahter data from both of them to insert in to field for submission to another database. When i go and use the mysql_fetch_assoc() for the query it gives me an error. I did some research and I cannot find anything that really pertaied to my issue. I was wondering if i could get some help.
<?php
$connect = mysql_connect("localhost","***********","*********") or die ("Couldn't Connect"); //host,username,password
mysql_select_db("virtua15_jo151") or die ("Could not find database");
$query = mysql_query("SELECT jos_comprofiler.firstname, jos_comprofiler.lastname, jos_users.username, jos_comprofiler.cb_country, jos_users.email FROM jos_users LEFT JOIN jos_comprofiler ON jos_users.id=jos_comprofiler.id WHERE jos_users.id = " . mysql_real_escape_string($id));
if ($row = mysql_fetch_assoc($query))
{
$firstname = $row['firstname']; //This is whatever the field is called in your table
$lastname = $row['lastname'];
$loginname = $row['jos_users.username'];
$country = $row['cb_country'];
$email = $row['email'];
}
?>
below this is all HTML I can include that if need be,
Something is wrong with query check with print mysql_error();
Check query. quote properly values assigned in query.
$query = mysql_query("SELECT jos_comprofiler.firstname, jos_comprofiler.lastname,
jos_users.username, jos_comprofiler.cb_country, jos_users.email
FROM jos_users LEFT JOIN jos_comprofiler ON jos_users.id=jos_comprofiler.id
WHERE jos_users.id ='".mysql_real_escape_string($id)."'");