I'm trying to create a basic PHP/MySQL test script. When I attempt to print the two tables in the database I created ('andrew'), the PHP output returns none. However, when I run the same command ('show tables;') through the MySQL 5.6 Command Line, both tables are returned. Any idea what I'm doing wrong?
<?php
$host='localhost';
$username='root';
$password='*****';
$database='andrew';
$connect=mysqli_connect($host,$username,$password,$database);
if(mysqli_connect_errno())
{echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
else{echo 'Connected to MySQL! </ br>';}
$result=mysqli_query($connect,'show tables;');
if(!$result){
echo 'Nope';}
else{echo '<p>Result!!!</p>';}
echo '<p>Tables in database:</p>';
echo "<ul>";
while($row = mysqli_fetch_assoc($result)){
print_r ($row);}
echo "</ul>";
?>
Try run this sql in your mysql command line:
use mysql;
grant all on andrew.* to 'root'#'localhost' identified by 'root password';
Then again try to run your script.
The script you have is working properly.
DETAILS:
$host='localhost';
$username='root';
$password='';
$database='test';
OUTPUT:
Connected to MySQL!
Result!!!
Tables in database:
Array ( [Tables_in_test] => tester )
<?php
$host='localhost';
$username='root';
$password='*****';
$database='andrew';
$connect=mysqli_connect($host,$username,$password,$database);
mysqli_select_db($connect,$database);
if(mysqli_connect_errno()) {
echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
}
else {
echo 'Connected to MySQL! </ br>';
}
$result=mysqli_query($connect,'your sql query');
if(!$result){
echo 'Nope';
} else{
echo '<p>Result!!!</p>';
}
echo '<p>Tables in database:</p>';
echo "<ul>";
while($row = mysqli_fetch_array($result)){
print_r($row);
}
echo "</ul>";
?>
Related
I wan to insert data to mysql table from another database which is connected via ODBC.But I cannot enter into the while loop, here is my code -
N.B: For security I dont provide db name, user and pass.
ODBC connection declared as 'connStr'
<?php
$connStr = odbc_connect("database","user","pass");
$conn = mysqli_connect("server","user","pass","database");
//$result_set=mysqli_query($conn,$datequery);
//$row=mysqli_fetch_array($result_set);
echo "<br>";
echo "<br>";
$query="select cardnumber, peoplename, creditlimit, ROUND(cbalance,2) as cbalance, minpay from IVR_CardMember_Info
where cardnumber not like '5127%'" ;
$rs=odbc_exec($connStr,$query);
$i = 1;
while(odbc_fetch_row($rs))
{ //echo "Test while";
$cardnumber=odbc_result($rs, "cardnumber");
$peoplename=odbc_result($rs, "peoplename");
$creditlimit=odbc_result($rs, "creditlimit");
$cbalance=odbc_result($rs, "cbalance");
$minpay=odbc_result($rs, "minpay");
$conn = mysqli_connect("server","user","pass","database");
$sql= "INSERT INTO test_data(cardnumber, peoplename, creditlimit, cbalance, minpay) VALUES ('cardnumber', 'peoplename', 'creditlimit', 'cbalance', 'minpay') ";
if(!(mysqli_query($conn,$sql))){
//echo "Data Not Found";
echo "<br>";
}
else{
echo "Data Inserted";
echo "<br>";
}
echo $i++ ;
}
echo "<br>";
odbc_close($connStr);
?>
How can I solve this?
If you really want to understand why you can't enter while() {...}, you need to consider the following.
First, your call to odbc_connect(), which expects database source name, username and password for first, second and third parameter. It should be something like this (DSN-less connection):
<?php
...
$connStr = odbc_connect("Driver={MySQL ODBC 8.0 Driver};Server=server;Database=database;", "user", "pass");
if (!$connStr) {
echo 'Connection error';
exit;
}
...
?>
Second, check for errors after odbc_exec():
<?php
...
$rs = odbc_exec($connStr, $query);
if (!$rs) {
echo 'Exec error';
exit;
}
...
?>
you can do it with just SQL check it here, like:
INSERT INTO test_data(cardnumber, peoplename, creditlimit, cbalance, minpay)
SELECT cardnumber, peoplename, creditlimit,
ROUND(cbalance,2) as cbalance, minpay from IVR_CardMember_Info
where cardnumber not like '5127%'
I have been working on a website which has a xampp server and a database called users with a table called AccountDetails. About a year ago I got it to work perfectly, but the server I was using then required MySQL not MySQLi. Now I have to use MySQLi and can't even get the simplest of sql's SELECT function to work, any ideas would be much appreciated.
<?php
$link = mysqli_connect("localhost:3306", "root","", "users");
if(mysqli_connect_errno($link)){
echo "MySql Error: " . mysqli_connect_error();
} else {
echo"Connection Successful <br></br>";
}
echo("Check if still working <br></br>");
// -----------------------------//
echo("Its running <br></br>");
$result = $link->query("SELECT ID, UserName FROM AccountDetails");
return $result->result();
var_dump($result);
mysqli_close($link);
?>
The Query itself works when I plug it into the phpmyadmin SQL section and it returns the values that I expect it too.
I've spent days looking online for different answers but none of them work, and the var_dump only gives me "bool(false)" which I don't think I should be getting.
You can try this code
<?php
$link = mysqli_connect("localhost:3306", "root","", "users");
if(mysqli_connect_errno($link)){
echo "MySql Error: " . mysqli_connect_error();
} else {
echo"Connection Successful <br></br>";
}
echo("Check if still working <br></br>");
// -----------------------------//
echo("Its running <br></br>");
$sql_select = "SELECT * FROM AccountDetails";
$result = $link->query($sql_select);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "UserName: " . $row['UserName']. "<br>";
}
} else {
echo "No Records";
}
$link->close();
?>
Heres my code. Simple.
<?php
echo 'start<br>';
//Do the conntection
$checkconnection = mysql_connect('localhost', 'root', 'rootpass');
//Check if it's valid
if(!$checkconnection) {
echo 'CheckFailed';
} else{
echo 'CheckSucess';
}
echo 'end'; ?>
but I only can see 'start'. There is no 'CheckFailed', 'CheckSucess', 'end'
What should I do?
I already install mysql, create database, create tables, of course.
<?php
// Create connection
$con=mysqli_connect("localhost","root","root","database");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
return false;
}
$result = mysqli_query($con, "SELECT * FROM table;");
?>
When moving data from on table to another I get Error in query: Duplicate entry '0' for key 'PRIMARY'
I dont care to copy the primary key I would like each table to have its own primary key- this table will just hold data to be processed,checked and released by person them moved to a final table that will contain all processed data.
<basefont face="Arial">
<title>QA-1160 Search</title>
</head>
<body>
<?php
// include the page Header
include('header.php');
?>
<?php
//retrieve session data
echo $_SESSION['mnumber'];
echo "<P>";
$mnumber=$_SESSION['mnumber'];
$amnumber=$mnumber;
$mnumber=" '".$mnumber."' ";
// set database server access variables:
$host = "localhost";
$user = "test";
$pass = "test";
$db = "test";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database :)!");
// create query
$query = "insert into testingqa1160 (material, test, sample, frequency, stp, rtr, notes, usl, lsl) SELECT material, test, sample, frequency, stp, rtr, notes, usl, lsl FROM qa1160 WHERE material=";
$query=$query.$mnumber;
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// mysql_free_result($result);
// close connection
mysql_close($connection);
// clear session
session_unset();
session_destroy();
// load test data
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database :)!");
// create query
$query = "SELECT * FROM testingqa1160";
// $query=$query.$mnumber;
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<center><table cellpadding=5 border=1>";
echo "<tr>";
echo "<center>";
echo "<td>"."ID"."</td>";
echo "<td>"."Material"."</td>";
echo "<td>"."Test"."</td>";
echo "<td>"."Sample"."</td>";
echo "<td>"."Frequency"."</td>";
echo "<td>"."STP"."</td>";
echo "<td>"."Release"."</td>";
echo "<td>"."Notes"."</td>";
echo "<td>"."LSL"."</td>";
echo "<td>"."USL"."</td>";
echo "</center></tr>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td>".$row[5]."</td>";
echo "<td>".$row[6]."</td>";
echo "<td>".$row[7]."</td>";
echo "<td>".$row[9]."</td>";
echo "<td>".$row[8]."</td>";
echo "</tr>";
}
echo "</table></center>";
echo "</center>";
}
else {
// no
// print status message
echo "<center><FONT SIZE=18>";
echo $_GET["mnumber"];
echo " Materail is not found! </font>";
echo "</center>";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
<td>Testing</td>
<?php
// include the page footer
include('footer.php');
?>
</body>
</html>
1.- Don't use mysql_* functions, they are deprecated, use mysqli or PDO
2.- Your table testingqa1160 need to have the auto-increment attribute to the id column
I am new to mysql and php. I am working on database programming with php with mysql but getting "no data base selected" error continuously. I found this error quite famous on internet. I tried every answer which were given to others having the same problem but nothing worked.
Here is my code:
if(!#mysql_connect('localhost','root','') || !#mysql_select_db ('a_database') ){
die ('Connection Error !');
}
$query = "SELECT `food`,`calories` FROM `food` ORDER BY `id`";
if($query_run=mysql_query($query)){
while($query_row = mysql_fetch_assoc($query_run))
{
$food = $query_row['food'];
$calories = $query_row['calories'];
echo $food.' has '.$calories.' Calories'.'<br>';
}
} else {
echo mysql_error();
}
This is the code that gives error. After some search on net. I made some bit of changes, but the result was same.
Changes that I made to first 3 to 4 lines:
$link = mysql_connect('localhost','root','');
if(!$link || !mysql_select_db ('a_database', $link) ){
die ('Connection Error !');
}
Please tell me what should I do to get rid of this problem, Thank you.
Try with this
<?php
// Create connection
$con=mysqli_connect("localhost","root","","a_database");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try this
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n"; // if everything is successful
And yes don't use mysql_* as it's deprecated, use mysqli_ or PDO.