This question already has answers here:
Deprecated: mysql_connect() [duplicate]
(12 answers)
Closed 6 years ago.
How can I make this query or script work properly with having these deprecated errors I am getting when using Wamp I am not getting the errors while using Xampp though. The errors or warning i'm getting is on this pic
this is my php script
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("SELECT gender as gender_occupation, COUNT(*) as total FROM hostel_blocks GROUP BY gender");
$rows = array();
while($r = mysql_fetch_array($result)) {
$row[0] = $r[0];
$row[1] = $r[1];
array_push($rows,$row);
}
print json_encode($rows, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
when i changed some connection code i got a blank page.
PHP mysql was deprecated in PHP 5.5 and removed starting PHP 7
You should move to mysqli instead.
Your code using mysqli would look like this:
<?php
$con = mysqli_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db( $con, "db");
$result = mysqli_query($con, "SELECT gender as gender_occupation, COUNT(*) as total FROM hostel_blocks GROUP BY gender");
$rows = array();
while($r = mysqli_fetch_array($result)) {
$row[0] = $r[0];
$row[1] = $r[1];
array_push($rows,$row);
}
print json_encode($rows, JSON_NUMERIC_CHECK);
mysqli_close($con);
?>
Related
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 4 years ago.
This is my code:
$con = mysqli_connect($databasehost,$dbname,$dbpassword) or die(mysql_error());
mysqli_select_db($con, $dbname) or die(mysql_error());
mysqli_query($con,"SET CHARACTER SET utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con,$query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($row = mysql_fetch_assoc($sth)){
$rows[] = $r;
}
print json_encode($rows);
}
What's the problem?
This is the error:
[19-Jan-2018 09:17:39 UTC] PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource on line 22
You are mixing mysqli and mysql.
The latter is deprecated as of version 5.5.0.
Try using mysqli_fetch_assoc($sth) instead of mysql_fetch_assoc($sth)
See http://php.net/manual/en/mysqli-result.fetch-assoc.php also.
Your problem is you are mixing mysqli and mysql.
Try to use mysqli_fetch_assoc($sth) instead of mysql_fetch_assoc($sth).
And, in mysqli_connect("host", "user", "password", "database"), you are missing the user name.
// (1) make sure $username is defined.
$con = mysqli_connect($databasehost,$username,$dbname,$dbpassword) or die(mysqli_error());
// check to use mysqli_, not mysql_
mysqli_select_db($con, $dbname) or die(mysqli_error());
mysqli_query($con,"SET CHARACTER SET utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con,$query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
// (2) Use mysqli_fetch_assoc instead of mysql_fetch_assoc.
while($row = mysqli_fetch_assoc($sth)) {
// (3) $r is not defined. Use $row.
$rows[] = $row;
}
print json_encode($rows);
}
*The condition for "die" had been left out of my code by mistake when I copied it to the question. I put it back in.
I know this question might seem repetitive, but I have not found an answer in any of the other questions. I am trying to create a drop-down list based off a column in a database. I have tried two different ways and neither gave me correct results. Does anyone know a correct way of doing this?
The first way I saw in other StackOverflow answers (Fetching data from MySQL database to html drop-down list, Fetching data from MySQL database to html dropdown list). My code is below:
<?php
$connect = mysql_connect('localhost', 'root');
if ($connect == false)
{
die ("Unable to connect to database<br>");
}
$select = mysql_select_db('ViviansVacations');
if ($select == false)
{
die ("Unable to select database<br>");
}
$query = "SELECT * FROM Destinations";
$result = mysql_query($query);
?>
<select name="select1">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='". $row['Europe'] ."'>" .$row['Europe'] ."</option>" ;
}
?>
</select>
NetBeans sends me an error saying that "Text not allowed in element 'select' in this context".
The second way I tried:
<?php
$connect = mysql_connect('localhost', 'root');
{
die ("Unable to connect to database<br>");
}
$select = mysql_select_db('ViviansVacations');
{
die ("Unable to select database<br>");
}
$query = "SELECT * FROM Destinations";
$result = mysql_query($query);
?>
<select name="select1">
<?php
while ($line = mysql_fetch_array($result))
{
?>
<option value="<?php echo $line['Europe'];?>"> <?php echo
$line['field'];?> </option>
<?php
}
?>
</select>
This code did not produce any errors. However, inside the form were the opening php lines followed by an empty drop down box:
"); } $select = mysql_select_db('ViviansVacations'); { die ("Unable to select database
"); } $query = "SELECT * FROM Destinations"; $result = mysql_query($query); ?>
So there are many problems with your approach. First of all you are using a deprecated mysql_* functions which is bad idea and second you are not debugging your database connection properly :
$connect = mysql_connect('localhost', 'root');
{
die ("Unable to connect to database<br>");
}
In above code the die statement will always execute stopping further execution.
Also make sure the database ViviansVacations and table Destinations and column Europe exists with correct names(Follow standards and try to use all small letters for database/table/column naming)
The correct mysqli_* approach is(tested locally and the select box forms correctly) :
<?php
$db = 'ViviansVacations';
$mysqli = new mysqli('localhost', 'root', '', $db);
if($mysqli->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
$query = "SELECT * FROM Destinations";
$result = mysqli_query($mysqli, $query);
?>
<select name="select1">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['Europe'] . "'>" . $row['Europe'] . "</option>";
}
?>
</select>
There are couple of things, you to verify and correct.
First of all, your database connection code, that doesn't seems to be correct. I didn't see any condition on which you are suppose to invoke die.
$connect = mysql_connect('localhost', 'root');
{
die ("Unable to connect to database<br>");
}
From the above code, below line will execute all the time
die ("Unable to connect to database<br>");
You should correct your database connection code to below :
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
You can refer mysql_connect for the usage.
Also, verify your file extension is .php
Mainly your problem is database connection. Try this -
<?php
mysql_connect("localhost", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("your_db");
$result = mysql_query("SELECT * FROM your_table");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row);
echo "<br>";
}
mysql_free_result($result);
?>
There could be few issues:
Use mysqli_connect instead of mysql_connect because mysql is depreciated
Make 3rd parameter as empty `mysqli_connect('localhost', 'root', '');//as per standards
Debug your code for example: (print_r($connect), execute your query in phpmyadmin, print_r($result) and then in loop print_r($row).
At end, I am sure you will get your desired result and also you will know in detail that what was happening.
You might want to try to use this ..The dept_id and the description u have to change according to your database .Hope this will help you
<?php
$sql = mysql_query("SELECT dept_id ,description FROM department
ORDER BY dept_id ASC");
db_select($sql,"dept_id",$dept_id,"","-select Department-","","");
?>
so I am new in StackOverflow :), I have a problem to answer a question in php5,
This is the question :
Create a PHP 5.4 script to check availability of many Sites (via Echo Protocol):
* Get the list of sites (domains) from MySQL database.
so I write this script and I want if it's the good answer :
<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysql_free_result($result);
?>
try something like this
$con = mysqli_connect(‘hostname’, ‘username’, ‘password’, ‘database’);
$result = mysqli_query($con , "select column_name from table_name");
while($data = mysqli_fetch_array($result))
{
echo $data['column_name'];
}
this will tell you the databases that have rights to be connected to
mysql_connect('localhost') or die ("Connect error");
$res = mysql_query("SHOW DATABASES");
while ($row = mysql_fetch_row($res)) {
echo $row[0], '<br/>';
}
You need to select a database before you use mysql_query.
$connection = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
//Write code to Check the connection
mysql_select_db('database_name', $connection);
//Write code to query
But as Robin mentioned in the comment, mysql* apis are deprecated. Use Mysqli or Pdo_Mysql. details here: http://in3.php.net/mysql_select_db
I have been trying for the past while to echo out the result of a a MySQL query in PHP. I can't seem to get it to work so I am a bit lost. I know for a fact that the query works as I have done it in PHPMYADMIN and it is working fine, however whenever I load the webpage, nothing is outputted. For the purposes of this question I have generalized the things in the query as I obviously don't want anyone accessing my database. This is what I have tried:
<?php
$connect = mysql_connect('host', 'user', 'password', 'dbname');
if (!$connect) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('dbname')) {
die('Could not select database: ' . mysql_error());
}
$result = mysql_query('SELECT SUM(row name) FROM `table`');
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result);
mysql_close($connect);
?>
I look forward to your replies.
mysql_result need the second parameter (The row number from the result that's being retrieved).
echo mysql_result($result, 0);
you have to change
echo mysql_result($result);
by
echo mysql_result($result,0);
or you can use
print_r(mysql_fetch_assoc($result));
Try this
echo mysql_result($result,0);
For more information, please give a look on http://www.php.net/mysql_result
or you can use mysql_fetch_row instead of mysql_result.
$row = mysql_fetch_row($result);
echo $row[0];
You may use this following format:
Give your Sql command like this:
$result = mysql_query('SELECT SUM(row name) as new_column_name FROM `table`');
then use this loop
while($fetched_values = mysql_fetch_array($result)){
echo $fetched_values['new_column'];
}
In your mysql_connect you have provided 4 parameters. it accepts 3 params
host, username and password. dbname is given inside mysql_select_db
as you can see from official docs. So remove the 4th param from mysql_connect function.
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
http://php.net/function.mysql-connect
$db_selected = mysql_select_db('foo', $link);
http://www.php.net/manual/en/function.mysql-select-db.php
but these mysql_* function will be deprecated use mysqli instead.
pass a row number as second parameter in mysql_result
echo mysql_result($result, 1);
or
use print_r(mysql_fetch_array($result));
where $result is your result resource
if you have any doubt you may refer my following example program.
<?php
$connect = mysql_connect('localhost', 'user_name', 'password', 'db_name');
if (!$connect)
{
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('db_name'))
{
die('Could not select database: ' . mysql_error());
}
$row = mysql_query('SELECT (Tamil+English+Maths) as total FROM table_name');
if (!$row)
{
die('Could not query:' . mysql_error());
}
while($fetched_row = mysql_fetch_array($row)){
echo $fetched_row['total'];
}
mysql_close($connect);
?>
here Tamil,English and Maths are the column values of my table
I have this code:
<?php
$con = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(DB_NAME, $con);
$result = mysql_query("SELECT SUM(colname) FROM profits");
$total = reset (mysql_fetch_assoc($result));
mysql_close($con);
echo $total;
?>
And its outputting an Resource id #5. Any help? Thanks in advance!
Don't use reset() like that - if you need a single value, just use list():
$result = mysql_query("SELECT SUM(colname) FROM profits");
list($total) = mysql_fetch_array($result);
reset() does not work as you expect on associative arrays: https://bugs.php.net/bug.php?id=38478
You're trying to echo an associative array with echo $total;. Instead just echo the first (and probably only based on your query) item.
$row = mysql_fetch_assoc($result);
echo $row[0];