Not able to Pull from database and display in PHP - php

Him I'm trying to pull the max value from a row to display it in a alert message. But It gives 2 errors ..
Warning: mysql_query() [function.mysql-query]: Access denied for user 'SYSTEM'#'localhost' (using password: NO) in C:\wamp\www\test\test1.php on line 20
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\wamp\www\test\test1.php on line 20
please Help
<html>
<title> Test</title>
<head>
</head>
<body>
<form method="POST" action="">
<h1>Button to display data</h1>
<input type="submit" name="submit" value="PULL">
</form>
</body>
<?php
$no=20;
if(isset($_POST['submit']))
include ('airlineDB2.php');
{
$select=mysql_query("select MAX(ticketno) from ticketbook");
print '<script type="text/javascript">';
print 'alert("The no is '. $select.' is already registered")';
print '</script>';
}
?>
</html>
THIS is the connection code to the database
and i have saved it as airlineDB2.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dberror1 = "Could not connect to database";
$link = mysql_connect($dbhost, $dbuser,$dbpass) or die ($drerror1);
$selectdb = mysql_select_db('airlinedb') or die ($drerror1);
?>

Connection to database does not work. You have a typo, that's why you don't see the error:
$dberror1 = "Could not connect to database";
but later on you use
$link = mysql_connect($dbhost, $dbuser,$dbpass) or die ($drerror1);
$dberror1 != $drerror1

<?php
$no=20;
if(isset($_POST['submit']))
include ('airlineDB2.php');
{
make the include after the curly bracket

Related

how to provide information from a mysql database on a website using php

I have a problem with providing infromation from a mysql database through php. I am working with brackets and always when I try to connect with my database (opening my connection, giving information about my server, port and the name of the dtabase -> shown in the code below) nothing happens. I can not administrate my database with phpmyadmin I know that would be easy. I also have xamp and a apache server working, but then I would have to use phpmyadmin to connect.
But is there a way to connect the database (created with mysql workbench running on localhost 127.0.0.1 and port 3306) only like that with my php file. Or is the problem that I have a mistake in my code?
Code:
File one: db_connection.php
<!DOCTYPE html>
<html>
<header>
<center> <h1>My New Website </h1> </center>
</header>
<body>
<?php
//die echo Funktion, ist dafür da, dass etwas in den Browser ausgegeben wird (same as Syso)
//echo "Hello World!";
function OpenConnection()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "dml_final";
$connection = new mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname)
or die ("Connection failed: %s\n". $connection -> error);
return connection;
}
function CloseConnection()
{
$connection -> close();
}
?>
</body>
</html>
File two: index.php
<?php requires 'db_connection.php'; ?>
<!DOCTYPE html>
<html>
<header>
<center> <h1>My New Website </h1> </center>
</header>
<body>
<?php
$connection = openConnection();
echo "Connection successfully!";
CloseConnection($connection);
?>
</body>
</html>
If you want to use your current way, change function to public function so that it is globally available across your website, I recommend the following method instead:
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "dml_final";
$connection = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
if(!$connection) {
die("Connection failed:".mysqli_connect_error());
}
And you include this in all of your PHP files. To close the connection, simply run a mysqli_close($connection);. I don't know about you, but I find this way more simple.

PHP MSQL server syntax error

I get this error when trying to get this details page for a project to work. Its for school and I dont really understand PHP that well yet.
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"
Here is the code for that page.
<?php
require_once('connection.php');
mysqli_select_db($conn, $dbname);
$recordID = $_GET['recordID'];
$query_Shoe_Details = "SELECT * FROM Products WHERE Shoe_Brand = $recordID";
$Shoe_Details = mysqli_query($conn, $query_Shoe_Details) or die(mysqli_error(($conn)));
$row_Shoe_Details= mysqli_fetch_assoc($Shoe_Details);
$totalRows_Shoe_Details = mysqli_num_rows($Shoe_Details);
?>
<!DOCTYPE html>
<html>
<head>
<title>details</title><?php include 'connection.php';?>
</head>
<body>
<p>Product Name: <?php echo $row_Shoe_Details['Product_Name']; ?></p>
<p><img src=
"images/%3C?php%20echo%20$row_Shoe_Details['Image_Name'];%20?%3E"></p>
<p>Description: <?php echo $row_Shoe_Details['Product_Description']; ?></p>
<p>Price: $<?php echo $row_Shoe_Details['Product_Price']; ?></p><?php
mysqli_free_result($Shoe_Details);
?>
</body>
</html>
change your query as, use single quotes
$query_Shoe_Details = "SELECT * FROM Products WHERE Shoe_Brand = '$recordID'";
Also remove this <?php include 'connection.php';?>, No need to include again
Pl. prepare connection.php file in followng way like
$dbHost = '';
$dbUser = '';
$dbPass = '';
$dbName = '';
// setting up the web root and server root for
// this shopping cart application
$con=mysql_connect('','','');
if(!$con)
{
die('connection failed');
}
$db=mysql_select_db('',$con);
if(!$db)
{
die('db is not selected');
}
pass proper value in this syntax save it and in your code remove second line and test then and give feedback

Why am I getting an access denied error when connecting to MySQL db from php page

EDIT: I found the problem. The connection problem was located on the other page I was redirecting to, which had the wrong credentials, I'm an idiot.
This is my first time asking a quesiton in here, so bear with me.
I wanted to test to see whether or not I am able to insert data into my MySQL database through my .php page. Although I seemingly can't connect to my database, even though the username, password and so on are all correct. I use the same credentials, when I log on to the local instance trough MySQL Workbench.
The error i get in my browser says this:
Connection failed: Access denied for user 'root'#'localhost' (using password: YES)
Also this is my first time coding php, so the code is probably littered with errors, feel free to point them out.
Here's my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
function prepareInput($input){
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
}
$servername = "localhost";
$username = "root";
$password = "1234";
$dbname = "praktikdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//---------------------------------------
$username1 = $password1 = "";
$errUsername = $errPassword = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(empty($_POST["username"])){
$errUsername = "Username is required";
}
else{
$username1 = prepareInput($_POST["username"]);
}
if(empty($_POST["password"])){
$errPassword = "Password is required";
}
else{
$password1 = prepareInput($_POST["password"]);
}
$sql = "INSERT INTO users (username, password) VALUES('$username1', '$password1')";
$result = $conn->query($sql);
if(!$result) die("Something went wrong". $conn->error);
$result->close();
$conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Registration</title>
</head>
<body>
<form action="login.php" method="post">
Username: <input type="text" name="username">
<span class="error"> <?php echo $errUsername?></span>
<br>
Password: <input type="text" name="password">
<span class="error"> <?php echo $errPassword?></span>
<br>
<input type="submit" name="btnRegister" name="Register"/>
</form>
</body>
</html>
Your code is giving you a credentials problem (detected by your "Check connection" piece of your code), and that is derived of the users configuration in your MySQL Workbench. You need to configure the user you will use for your PHP connection to MySQL, along with the password, and the server the user connecting will be limited to (in this case is the localhost), and the privileges your user will have.
Also, and this is just for connecting to your database (for now consider that you won't execute a SQL query in your code), you can check if the connection it's ok with just your username, the password and the server name.
<?php
/* $servername, $username and $password goes here */
$conn = new mysqli($servername, $username, $password);
if($conn->connect_error) {
die("Connection failed:" $conn->connect_error);
}
echo "Connection successful";
?>

Why isn't my mysql query from python shell going through to the web hosting database

ok so I have the following code that I am running in the python shell:
import MySQLdb
db = MySQLdb.connect(host = "xxxx",user="xxxx"password="xxxx",db="xxxx")
cur = db.cursor()
cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))
Fairly sure the connection part is working, I'll get an error if I enter in the wrong value, or if I deny access to the database for my computer's IP address.
on the webhosting server, I have the following basic index.php file, which I have tested on a server on my computer, and I know works. when I go to the website domain, I get the following error: "Database query failed."
Any ideas why the MySQL query isn't working? My webhosting is Cpanel with godaddy.com, should I look for something else?
<?php
$dbhost = "xxxx";
$dbuser = "xxxx";
$dbpass = "xxxx";
$dbname = "xxxx";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); /*1*/
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
<?php
$query ="SELECT * FROM qqqq"; /*2*/
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed.");
}
?>
<!DOCTYPE html PUBLIC >
<html lang="en">
<head>
<title></title>
</head>
<body>
<ul>
<?php /*3*/
while($subject = mysqli_fetch_assoc($result)){
?><li><?php echo $subject['asdf'];?></li>
<?php
}
?>
</ul>
<?php
mysqli_free_result($result); /*4*/
?>
</body>
</html>
<?php
mysqli_close($connection); /*5*/
?>
You should call db.commit() to have it complete. By default, autocommit is turned off.
You also have an error in your code. The SQL should be a string.
Shouldn't the cursor execute be calling a string? You don't have quotes around your sql statement.
This line without quotas is incorrect:
cur.execute(CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))
It should be
cur.execute("CREATE TABLE qqqq (asdf VARCHAR(20),fdsa VARCHAR(20))")
So test in your database whether you really have table qqqq.
You could install SQL Buddy or phpMyAdmin.

The Connection is Successful but nogetting the data into website mysql

We are Trying to Connect php & mysql we got connection successful but we are not receiving the data any ideas please guide
Also we are using mysql and php
The Connection is Successful but nogetting the data into website
enter code here`Please let me know who to correct it<html>
<head>
<title> Welcome to PDM</title>
</head>
<body>
<div>
<centre>
Good For Checking The Prices
</centre>
<?php>`
$db_host = "localhost";
$db_username = "wikiacwj_price";
$db_pass = "";
$db_name = "wikiacwj_price";
mysql_connect("$db_host","$db_username","$db_pass") or die ("Please Try Again");
mysql_select_db("wikiacwj_price") or die ("no data");
$sql = mysql_query("SELECT * FROM price_comparsion where product_name='ok'");
//write the results
while ($row = mysql_fetch_array($sql)); {
echo $row['product_name'];}
?>
</body>
</html>
The Semicolon immediately after your while statement telling the while loop to do ... nothing :)
...
while ($row = mysql_fetch_array($sql)) {
echo $row['product_name'];
}
...
shoud do the trick.

Categories