I got this error in mysqli
mysqli_select_db() expects exactly 2 parameters, 1 given in
/home/u751513549/public_html/recent.php on line 6
This is the code
<?php // Connects to your Database
mysqli_connect("localhost", "u751513549_liker", "xxxxxxxx") or die(mysqli_error());
mysqli_select_db("u851654599_liker") or die(mysqli_error());
$data = mysqli_query("SELECT *
FROM token_all
ORDER BY RAND() LIMIT 0,9; ") or die(mysqli_error());
Print "<table";
while ($info = mysqli_fetch_array($data)) {
Print "<tr>";
Print " <a href=\"https://www.facebook.com/" . $info['id'] . "/\"/> <img src=\"https://graph.facebook.com/" . $info['id'] . "/picture\"/></a>";
}
Print "</table>";
Please help me to fix this error I am a beginner.
Try this (I have added another parameter to mysqli_select_db):
$con = mysqli_connect("localhost", "u751513549_liker", "xxxxxxxx") or die(mysqli_error($con));
mysqli_select_db($con, "u851654599_liker") or die(mysqli_error($con));
Step 1: Create Connection
$conn = mysqli_connect("localhost","u751513549_liker","xxxxxxxx","u851654599_liker");
Step 2: Run your Query
$query= mysqli_query($conn,"Your Query") or die(mysqli_error($conn));
Step 3 : Fetch records
$result = mysqli_fetch_all($query);
Step 4 : Display Records
var_dump($result);
Related
Very basic but as a rookie I am struggling. The echo doesnt show any value, just the text. What am I doing wrong?
Connect.php:
<?php
$connection = mysqli_connect('test.com.mysql', 'test_com_systems', 'systems');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'swaut_com_systems');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>
Get.php:
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
echo ( 'SystemID: '.$result2);
?>
Assuming you have connected to the database successfully then the query is incorrect. You must wrap all text values in quotes like this
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username='test'";
$result2 = mysqli_query($connection, $query2);
Now the mysqli_query submits the query to the database where it is run and a result set built. To see the result set you need to read the result set back from the database using one of the fetch functions for example
$row = mysqli_fetch_assoc($result2);
echo 'SystemID: ' . $row['systemid'];
If there are more than one rows in the result set you must do that in a loop like this
while ($row = mysqli_fetch_assoc($result2)){
echo 'SystemID: ' . $row['systemid'];
}
You are printing the mysqli result object. In order to printthe result you have to use:
$row = mysqli_fetch_assoc($result2);
print_r($row);
You need to collect the results of the mysqli_query using the following:
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
while ($row = mysqli_fetch_assoc($result2))
{
echo "System ID is: " . $row['systemid'];
}
I want to make an API that when you access it it will show result in text format of how many rows are on my database.
Database info:
Username: predator_db
DB Name: predator_db
Table Name: database
I tried a couple of codes and I could not get it to work.
Code tried:
<?php
$con = mysql_connect("localhost","predator_db","PASS");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("predator_db", $con);
$result = mysql_query("select count(1) FROM database");
$row = mysql_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
mysql_close($con);
?>
Response Of Code: "Total Rows: " < Does not show how many rows. Error Log:
[05-Feb-2015 23:44:58 UTC] PHP Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/predator/public_html/api/resolver/number.php on line 2
[05-Feb-2015 23:44:58 UTC] PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/predator/public_html/api/resolver/number.php on line 10
You're trying to fetch the results from database... not from your actual database of predator_db.
I'll do it with the basics, but please look into MySQLi prepared statements and/or PDO.
$link = mysqli_connect("localhost", "predator_db", "PASS", "predator_db");
$result = mysqli_query($link, "select COUNT(id) AS count FROM `database`");
// make sure it worked
if(!$result) {
die('Error: ' . mysqli_error($link));
} else {
$num_rows = mysqli_fetch_assoc($result);
// echo it
echo "Rows: " . $num_rows['count'];
}
First off, it's not a good idea to name a table after a reserved keyword like database. However, if you are going to go that route, you will always have to place the name in backticks ``. So, your query should be
$result = mysql_query("select count(1) FROM `database`");
Also, look into MYSQLi, as the old MySQL driver is deprecated.
<?php
$link = mysqli_connect("localhost", "DB_USER", "DB_PASS", "DB_NAME");
$result = mysqli_query($link, "select COUNT(*) AS count FROM DB_NAME.DB_TABLE");
if(!$result) {
die('Error: ' . mysqli_error($link));
} else {
$num_rows = mysqli_fetch_assoc($result);
echo "Rows: " . $num_rows['count'];
}
?>
I'm executing following simple sql query:
SELECT people_picture, people_gender FROM people LIMIT 1 which in phpmyadmin gives a result.
My php code is the following:
$query = "SELECT people_picture, people_gender FROM people LIMIT 1";
$result = mysqli_query($con,$query);
$row = mysql_fetch_assoc($result);
$row1 = mysql_fetch_array($result);
print_r($row1);
print_r($row);
It doesn't give an error, but it just doesn't do anything.
You are mixing mysqli and mysql interfaces thats why you got stuck:
Try this please:
$con = mysqli_connect($host, $user, $pass, $dbase);
if(!$con){
echo 'Connection failed : '.mysqli_connect_error();
exit(0);
}
$query = "SELECT people_picture, people_gender FROM people LIMIT 1";
$result = mysqli_query($con,$query);
if(!$result){
echo 'Query failed : '.mysqli_error();
exit(0);
}
$row = mysqli_fetch_assoc($result); // mysql_fetch_assoc was the problem
print_r($row);
Use mysqli_fetch_assoc instead
Is this Query and display info all correct? Syntax-wise.
<?php
mysql_connect("HOST", "USERNAME", "PASSWORD") or die (mysql_error ());
mysql_select_db("DATABASENAME?!?!") or die(mysql_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysql_query($strSQL) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['COLUMNNAME'] . "<br />";
}
mysql_close()
?>
Use statement as
<?php
$conn=mysqli_connect("HOST", "USERNAME", "PASSWORD") or die (mysqli_error ());
mysqli_select_db("DATABASENAME",$conn) or die(mysqli_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysqli_query($strSQL) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
echo $row['COLUMNNAME'] . "<br />";
}
mysqli_close()
?>
Also I advice you to use Mysqli or Pdo instead of Mysql driver as it is deprecated.
There is no issue in your question or code. Syntax wise it is fine. But it is advised not to use mysql_* functions because it is deprecated.
The general way of using the code for your condition would be:
<?php
mysqli_connect("HOST", "USERNAME", "PASSWORD") or die (mysqli_error());
mysqli_select_db("DATABASENAME") or die(mysqli_error());
$strSQL = "SELECT * FROM TABLENAME";
$result = mysqli_query($strSQL) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
foreach ($row as $column => $value)
echo $column . " = " . $value . "<br />";
echo "<br/>";
}
mysqli_close();
?>
So, this iterates and suppose say you have four columns, and two rows, it gives an output like:
Column 1 Name = Value
Column 2 Name = Value
Column 3 Name = Value
Column 4 Name = Value
Column 1 Name = Value
Column 2 Name = Value
Column 3 Name = Value
Column 4 Name = Value
What is the best MySQL command to count the total number of rows in a table without any conditions applied to it? I'm doing this through php, so maybe there is a php function which does this for me? I don't know. Here is an example of my php:
<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("some command");
$row = mysql_fetch_array($result);
mysql_close($con);
?>
<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("select count(1) FROM table");
$row = mysql_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
mysql_close($con);
?>
Either use COUNT in your MySQL query or do a SELECT * FROM table and do:
$result = mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($result);
echo "There are " . $rows . " rows in my table.";
mysqli_num_rows is used in php 5 and above.
e.g
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$rowcount);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
Use COUNT in a SELECT query.
$result = mysql_query('SELECT COUNT(1) FROM table');
$num_rows = mysql_result($result, 0, 0);
you can do it only in one line as below:
$cnt = mysqli_num_rows(mysql_query("SELECT COUNT(1) FROM TABLE"));
echo $cnt;
use num_rows to get correct count for queries with conditions
$result = $connect->query("select * from table where id='$iid'");
$count=$result->num_rows;
echo "$count";
for PHP 5.3 using PDO
<?php
$staff=$dbh->prepare("SELECT count(*) FROM staff_login");
$staff->execute();
$staffrow = $staff->fetch(PDO::FETCH_NUM);
$staffcount = $staffrow[0];
echo $staffcount;
?>
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
echo "number of rows: ",$rowcount;
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
it is best way (I think) to get the number of special row in mysql with php.
<?php
$conn=mysqli_connect("127.0.0.1:3306","root","","admin");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="select count('user_id') from login_user";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
echo "$row[0]";
mysqli_close($conn);
?>
Still having problem visit my tutorial http://www.studentstutorial.com/php/php-count-rows.php
$sql = "select count(column_name) as count from table";
Well, I used the following approach to do the same: I have to get a count of many tables for listing the number of services, projects, etc on the dashboard. I hope it helps.
PHP Code
// create a function 'cnt' which accepts '$tableName' as the parameter.
function cnt($tableName){
global $conection;
$itemCount = mysqli_num_rows(mysqli_query($conection, "SELECT * FROM `$tableName`"));
echo'<h6>'.$itemCount.'</h6>';
}
Then when I need to get the count of items in the table, I call the function like following
<?php
cnt($tableName = 'projects');
?>
In my HTML front end, so it renders the count number
It's to be noted that I create the cnt() function as a global function in a separate file which I include in my head, so I can call it from anywhere in my code.