Strange behaviour trying to detect errors selecting a database - php

I have the following code in php:
<?php
$con = mysql_connect("localhost","john","john");
mysql_select_db("data", $con);
if (!$mysql_select_db) {
die ('Cannot use data : ' . mysql_error());
}
$result = mysql_query("SELECT * FROM test where huss='hussein'");
while ($row = mysql_fetch_array($result)) {
echo "ADSDAD";
echo "<br />";
}
if (!$con) {
die('ERROR Could not connect: ' . mysql_error());
}
mysql_close($con);
?>
I was able to connect to the MySQL server, but I can't seem to select the relevant database.
Being printed is the hard-coded text Cannot use data :, but no MySQL error message follows it. If the MySQL connection had failed then I would have expected a MySQL error message to appear after the hard-coded text.
What am I doing wrong?

The problem's here:
mysql_select_db("data", $con);
if (!$mysql_select_db) {
die ('Cannot use data : ' . mysql_error());
}
You don't have a variable $mysql_select_db, and it doesn't magically map to the result of the last time you called mysql_select_db.
So, instead:
$result = mysql_select_db("data", $con);
if (!$result) {
die ('Cannot use data : ' . mysql_error());
}
Or:
if (!mysql_select_db("data", $con)) {
die ('Cannot use data : ' . mysql_error());
}

Related

How to use Insert query in PHP code

I do not understand what is going wrong with code. The result is get is "connected successfully success Query failed". I tried few combinations and I get the same result. Please help me in solving this. Thanks in advance.
<?php
$link = mysql_connect('localhost', 'root1', '')
or die('Could not connect: ' . mysql_error());
if ($link) {
echo 'connected successfully';
}
$l = mysql_select_db('vtflix', $link) or die ('Could not select the database');
if ($l) {
echo ' success';
}
/*$varCNAME = 'John';
$varCONTENT = '4';
$varVID = '1';*/
$sql = "INSERT INTO mpaa(C_Name, ContentRating, V_ID) VALUES ('Jon', 4, 3)";
mysql_query($sql, $link) or die("Query failed");
$que = "SELECT * FROM mpaa";
$query = mysql_query($que, $link);
if (!$query) {
echo 'query failed';
}
while ($sqlrow = mysql_fetch_array($query, MYSQL_ASSOC)) {
$row = $sqlrow['C_Name'];
$nrow = $sqlrow['Content Rating'];
$mrow = $sqlrow['V_ID'];
echo "<br>" . $row . " " . $nrow . " " . $mrow . "<br>";
}
mysql_close($link);
?>
1.Don't use mysql_* library (deprecated from php5 onward + removed from php7) .Use mysqli_* OR PDO.
2.An example of mysqli_*(with your code)is given below:-
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
Note:- To check php version (either on localhost or on live server) create a file with name phpInfo.php, and just write one line code in that file:-
<?php
phpinfo();
?>
Now run this file and you will get the current php version.
Like this:- https://eval.in/684551
Here it seems that you are using deprecated API of mysql_* .
1) Check your PHP version
<?php phpinfo();exit;//check version ?>
2) avoid the usage of mysql use mysqli or PDO
3) change your db connection string with this :
new Mysqlidb($hostname, $username, $pwd, $dbname);
example with you code
<?php
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>

Why am I getting this error "Error: Query was empty"

I am trying to update my SQL database using a form through php, but i keep getting the error "Error: Query was empty".
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
mysql_query($sql, $con);
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
mysql_close($con);
?>
It also won't update my table and I don't know what I've done wrong. All help will be much appreciated. I am new to this as you can probably tell!
There is an error in your code first you are calling mysql_query($sql, $con); without any query in your $sql variable your $sql is blank ""
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
mysql_close($con);
?>
Remove mysql_query($sql, $con) query execution after db selection because $sql is empty,
Also put your update sql execution in IF conditions, because if its not true than again $sql will be empty and you will get same error again,...
<?php
$sql = "";
$con = mysql_connect("*******","*******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*******", $con);
// mysql_query($sql, $con); // <-- remove this
if (isset($_POST['STUDENT_FNAME'], $_POST['STUDENT_SNAME'],
$_POST['STUDENTNO'] ))
{
$sql="UPDATE STUDENT SET STUDENT_FNAME=('$_POST[STUDENT_FNAME]'),
STUDENT_SNAME=('$_POST[STUDENT_SNAME]')
WHERE STUDENTNO=
('$_POST[STUDENTNO]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record updated";
}
mysql_close($con);
?>

Mysql COUNT function not working

I have a database called articles and I just wanna print out the amount of rows in my database. This is my code:
<?php
$con = mysql_connect('link', 'database', 'password');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT COUNT(*) FROM articles");
$row=mysql_fetch_array($result)
?>
<p>The amount of rows is <?php echo $row ?>.</p>
When I test this code, is just gives me 'The amount of rows is .' as output. What am I doing wrong?
Edit: This is my full code now, but it still doens't work:
$con = mysqli_connect('url', 'database', 'password');
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
$result = mysqli_query($con, "SELECT COUNT(URL) AS row_count FROM articles");
if (!$result ) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysqli_error($con);
exit;
}
$row=mysqli_fetch_array($result)
?>
<p>The amount of rows is <?= $row['row_count'] ?>.</p>
The error: DB Error, could not query the database MySQL Error: No database selected
You're trying to print the row.
Use echo $row[0] instead
Also, as it's printing nothing, I suspect your query is failing, as FALSE will be echoed as nothing in PHP.
Sidenote: mysql_* is deprecated, consider using PDO instead.
EDIT:
full code below
<?php
$con = new mysqli('url', 'database', 'password', 'db_name');
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
$result = $con->query("SELECT COUNT(*) AS row_count FROM articles");
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . $con->error;
exit;
}
$row = $result->fetch_assoc();
?>
<p>The amount of rows is <?= $row['row_count'] ?>.</p>
You are echoing an array (which should give you error). Instead you can set alias for COUNT:
$result = mysqli_query($con, "SELECT COUNT(*) AS Total FROM articles");
And echo it like:
<p>The amount of rows is <?php echo $row['Total'] ?>.</p>
As others said, you're trying to print the array (hence the fetch_array), when you need to print something in the array.
Also, mysql_* is depreciated, I use mysqli_* which is very similar and easy to switch to.
Also, instead of *, try a fieldname in the count. I usually use the index, or primary key or something.
<?php
$link = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno($link)) {
echo 'Could not select database:' . DB_NAME;
exit;
}
$result = mysqli_query($link, "SELECT COUNT(fieldname) AS row_count FROM articles");
if (!$result ) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysqli_error($link);
exit;
}
$row=mysqli_fetch_array($result)
?>
<p>The amount of rows is <?= $row['row_count'] ?>.</p>

php mysql query string

I need help to query string from database please help.
<?php
$phone="8165526693#vtext.com";
$link = mysql_connect('localhost', 'root', 'toor');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('wizarddb')) {
die('Could not select database: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM Phone WHERE phone LIKE '%$phone%'");
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result(1); // outputs phone
mysql_close($link);
?>
I have issues with echo or is the query wrong?
Your error is on the echo.
echo mysql_result($result(1); // outputs phone
That SHOULD be
echo mysql_result($result[1]); // outputs phone

Cannot select from database?

I'm trying to select from my MySQL database, from the table Lines. I made it really simple, it should simply show all rows in the database. However I'm getting an error on line 16 (I put an * next to it) that says: "mysql_fetch_array(): supplied argument is not a valid MySQL result resource"
$con = mysql_connect("...","...","mypass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$result = mysql_query("SELECT * FROM Lines");
while($row = mysql_fetch_array( $result )) ***************
{
echo $row['Text'];
echo "<br />";
}
mysql_close($con);
What am I doing wrong? Thanks
Lines is a reserved word in MySQL. Use backticks.
SELECT * FROM `Lines`
Add mysql_error() after mysql_query() to see if there's any error, I'm guessing table Lines doesn't exist:
$con = mysql_connect("copoetry.db.6945202.hostedresource.com","dbname","mypass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$result = mysql_query("SELECT * FROM Lines");
echo mysql_error(); die;
while($row = mysql_fetch_array( $result )) ***************
{
echo $row['Text'];
echo "<br />";
}
mysql_close($con);

Categories