php mysql is this correct? - php

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

Related

Read value from database, echo

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'];
}

Error in mysqli statement

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);

MySQL Order By Rand() gives 1 result not 24

I want to display 24 unique id's but he is only giving me 1:
This is my php
<?php $sql = "SELECT id
FROM 15players
ORDER BY RAND()
LIMIT 24"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_array($result);
echo $row['id']; ?>
But if I execute this SQL in phpmyadmin,
It returns me 24 id's
So what am I doing wrong?
EDIT:
Got the right answer!
<?php $sql = "SELECT id
FROM 15players
ORDER BY RAND()
LIMIT 0,24";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($result)) {
echo $row['id'];
}
?>
mysql_fetch_array only returns 1 row you need to loop through the results.
while($row = mysql_fetch_array($result)) {
echo $row['id'];
}
If multiple results are fetched via a query, you need to iterate over it.
<?php
$sql = "SELECT id
FROM 15players
ORDER BY RAND()
LIMIT 24"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id'];
}
?>
Also mysql_* is deprecated, start using PDO.
mysql_fetch_array only get one row at a time. You need to loop through all of your results.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo $row['id']. '<br>';
}
You should be using mysqli or PDO. The mysql_ is deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.

Unable to count database users

I can't count my users in the database with my current configurations.
<?php
require_once 'scripts/app_config.php';
mysqli_connect(DATABASE_HOST,DATABASE_USER,DATABASE_PASS,DATABASE_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo "There are: ". $num . " users in the database";
mysqli_close($con);
?>
Your problem is this:
$num = mysql_num_rows($result);
Your query only returns 1 row, so I presume the problem you're having is that it always outputs: There are 1 users in the database.
You should instead use:
$num = mysqli_fetch_array($query)[0];
Either use COUNT(*) OR use mysql_num_rows
$query = "SELECT COUNT(*) FROM users";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$num = $row[0];
echo "There are: ". $num . " users in the database";
Note : mysql_* function is deprecated, move on mysqli_* function asap.

MySQL/Php: nonrepeating random selection with unique ID selectors

I'm new to using php and mysqp.
Using the code below, I am calling and echoing four names from a mysql database (in a 'names' column) without repeat.
However, what I'd like to do is assign a unique CSS id selector to each name.
The intent is to have four individually styled boxes around the page, each with a different name chosen randomly upon load.
What would be the best code to do this?
(Fyi I am using an xxamp installation.)
Thanks!
<?php
$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);
$sql = "SELECT * FROM names ORDER BY RAND() LIMIT 4";
$result = mysql_query($sql, $sandbox);
while ($row = mysql_fetch_array ($result)) {
$name1 = $row['Name'];
echo $name1 . '<br>' ;
}
Would this work for you:
<?php
$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);
$sql = "SELECT * FROM names ORDER BY RAND() GROUP BY Name LIMIT 4";
$result = mysql_query($sql, $sandbox);
$i = 0;
while ($row = mysql_fetch_array ($result)) {
echo '<div id=box"'.$i++.'">'.$row['Name'].'</div>';
}
This gives you an unique CSS selector using the primary key in your table?
This should work.
$sandbox = mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("sandbox", $sandbox);
$sql = "SELECT * FROM names ORDER BY RAND() LIMIT 4";
$result = mysql_query($sql, $sandbox);
$i = 0;
while ($row = mysql_fetch_array ($result)) {
$id = $row['ID'];
echo '<div id=box"'.$i++.'">your content</div>';
}

Categories