Mamp phpMyAdmin table query - mysql

I am having an issue were my code appears to be correct and am getting no errors querying or connecting to my database. "ErrorNumber 0:..."
But when i check phpMyAdmin it clearly has not queried. I have check all of my titles and table names, database name and everything is fine. My query will just not run or insert. here is my code:
mysql_connect('localhost', 'root', 'root', 'aliendatabase');
if (!$dbc) {
die('Error connecting to MySQL server.' . mysql_error());
}
$query = "INSERT INTO aliens_abduction(first_name, last_name, when_it_happened, how_long, how_many, alien_description, what_they_did, fang_spotted, other, email)" .
"VALUES('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', '$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
mysql_query($query, $aliendatabase);
echo "error number" . mysql_errno() . ": " . mysql_error() . "\n";
echo 'Connected Successfully';
mysql_close($dbc);

what is $dbc ?
It should rather be $dbc = mysql_connect(...) to test the connection.

Related

SQL query and output in PHP does not work - 500 Internal Server Error

i try to put a working SQL query in a PHP-file for my Cronjob. But i receive only a blank 500 Internal Server Error notification.
The SQL query works fine in PHPmyAdmin.
Here the code:
<?php
$con=mysqli_connect("HOST","DBUSER","DBPW","DBNAME");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT oc_order_product.order_id AS bestellnr, oc_order_product.quantity, oc_order_product.model, oc_order_product.name, oc_order.shipping_company, oc_order.shipping_firstname, oc_order.shipping_lastname, oc_order.shipping_city
FROM oc_order_product, oc_order
WHERE oc_order.order_id = oc_order_product.order_id
AND oc_order.order_status_id = 1
ORDER BY bestellnr, model");
while($row = mysqli_fetch_array($result))
{
echo $row['oc_order_product.order_id'] . "; " . $row['oc_order_product.quantity'] "; " . $row['oc_order_product.model'] "; " . $row['oc_order_product.name'] "; " . $row['oc_order.shipping_company'] "; " . $row['oc_order.shipping_firstname'] "; " . $row['oc_order.shipping_lastname'] "; " . $row['oc_order.shipping_city']; //these are the fields that you have stored in your database table
echo "<br />";
}
mysqli_close($con);
?>
The error_log is empty. Is something wrong with my code?
The error message, that i receive via mail ist:
Status: 500 Internal Server Error
X-Powered-By: PHP/5.6.19
Content-type: text/html; charset=UTF-8
Any idea?
Thanx
I can note that this is incorrect:
$row['oc_order_product.order_id']
The row has no column with that name. In fact, you have explicitly given the column the name bestellnr. So, you might try:
$row['bestellnr']
I can't guarantee that is the only problem, though.
That's the final code:
<?php
$pdo = new PDO('mysql:host=DBHOST;dbname=DBNAME', 'DBUSER', 'DBPW');
$sql = "SELECT oc_order_product.order_id AS bestellnr, oc_order_product.quantity, oc_order_product.model, oc_order_product.name, oc_order.shipping_company, oc_order.shipping_firstname, oc_order.shipping_lastname, oc_order.shipping_city
FROM oc_order_product, oc_order
WHERE oc_order.order_id = oc_order_product.order_id
AND oc_order.order_status_id = 1
ORDER BY bestellnr, model";
foreach ($pdo->query($sql) as $row) {
echo $row['bestellnr']."; ".$row['quantity']."; ".$row['model']."; ".$row['name']."; ".$row['shipping_company']."; ".$row['shipping_firstname']."; ".$row['shipping_lastname']."; ".$row['shipping_city']."<br />";
}
?>

Create a div on my website that shows database information with php

I need to create a div that only shows the information of the database, how should i connect the div to mysql so it only shows my database information?
I'm not sure I'm understanding your question... but I'll give it a try. The code below this will select whatever you tell it to from the database and echo it into a table.
<?php
$db = mysqli_connect('host', 'username', 'password', 'database');
$sql = "SELECT * FROM database"
mysqli_stmt_bind_param($db, "params here", your info here);
$res = mysqli_stmt_execute($sql);
mysqli_stmt_close($sql);
mysqli_close($db);
echo "<table>";
while($row = mysqli_fetch_array($res)){ //Creates a loop to loop through results
echo "<tr><td>" . $row[''] . "</td><td>" . $row[''] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>";
?>

Table dosent exist

I keep getting this error when i try and run some mysql code from a php file on my server :
Table 'leaderboard.u' doesn't exist
The thing is that the table leaderboard does exist . Its been setup the same way that I have it setup in xampp . It also works on xampp and worked on the server until recently .
What could be causing this ?
EDIT :
<?php
include("../../config/connect.php");
if(!empty($_GET['id']) && !empty($_GET['data']))
{
$id = $_GET['id'];
$data = $_GET['data'];
$key = $_GET['key'];
$result = mysqli_query($connection,"update userbadges set $key='$data' where id='$id'");
if(!$result) {
echo mysqli_error($connection);
exit;
}
echo 'database update ' . mysqli_affected_rows($connection) . ' rows';
}
?>

MySQL column mismatch error on very basic query

So I'm getting this error:
Error: Column count doesn't match value count at row 1
(Very common, and I've checked through google, and my issue is that most of the issues are actual comlumn mismatches as it describes)
My location table has "user", "latitude", "longitude", "posttext", user and posttext are both varchar, and lat and long are int. For the time being, I'm just trying to insert values with user and posttext values. I've taken the query out of my php, and run it in the SQL part of phpmyadmin and it runs fine, so I'm not sure why I'm getting the error.
A php form supplies the post data from text boxes, and this is the php processing code:
<?php
error_reporting(E_ALL);
$username = $_POST['user'];
$textToPost = $_POST['textToPost'];
$con = mysql_connect("127.0.0.1","*","*");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test");
$sql= "INSERT INTO location(user, posttext)
VALUES ('.$username.,.$textToPost.')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
echo $username;
echo $textToPost;
?>
So I'm hoping it's a very basic syntax error on my part, but could someone help?
You're missing some quotes:
$sql= "INSERT INTO location(user, posttext)
VALUES ('".$username."','".$textToPost."')";
Shouldn't the query be like this?
$sql= "INSERT INTO location(user, posttext)
VALUES ('".$username."','".$textToPost."')";
Try with
$sql= "INSERT INTO location(user, posttext)
VALUES ('".$username."','".$textToPost."')";
Remember you MUST always sanitize user input before using it in a query!!
It could be better using prepared statements...

Can I construct a php page to run a query check against 30 mysql databases?

I host at hostgator and have about 30 mysql databases (all different websites that sit on the same server). For the last year.. no problems and suddenly, the last 2 days, I've seen 5 - 10 of these databases marked as 'crashed' and they return no results... so my websites display no info. I have to run a "repair table mytable" to fix these and then they work great again.
Instead of logging in to go through the databases 1 by 1 every morning, is there a way I could setup a php page to connect to all 30 databases and run a simple select statement.. and if it works, return
"database db1 is working"
"database db2 is working"
and then when not working, return
"no reply from db3"
....or something similar?
Thanks!
There's no reason you couldn't have a script that lists all of your databasenames and login credentials, and try to connect in turn to each:
$logins = array(
array('dbname' => 'blah', 'user' => 'username1', 'password' => 'password1'),
array('dbname' => 'yuck', ....)
...
);
$failures = array();
foreach ($logins as $login) {
$con = mysql_connect('servername', $login['user'], $login['password']);
if (!$con) {
$failures[] = $login['dbname'] . " failed with " . mysql_error();
continue;
}
$result = mysql_select_db($login['dbname']);
if (!$result) {
$failures[] = "Failed to select " . $login['dbname'] . ": " . mysql_error();
continue;
}
$result = mysql_query("SELECT something FROM sometable");
if (!$result) {
$failures[] = "Faile to select from " . $login['dbname'] . ": " . mysql_error();
continue;
}
if (mysql_num_rows($result) != $some_expected_value) {
$failures[] = "Got incorrect rowcount " . mysql_num_rows($result) . " on " . $login['dbname'];
}
etc....
mysql_close();
}
if (count($failures) > 0) {
echo "Failures found: "
print_r($failures);
}
You should be able to do something like the following:
<?php
//connect to database
mysql_connect('database','user','password');
//get all database names
$result = mysql_query("show databases;");
//iterate over all databases returned from 'show databases' query
while($row = mysql_fetch_array($result)) {
//DB name is returned in the result set's first element. select that DB
mysql_selectdb($row[0]);
//get all tables in the database
$query = "show tables;";
$result2 = mysql_query($query);
echo "Query: (".$row[0].")$query\n";
echo mysql_error();
//iterate over all tables in the current database
while($row2 = mysql_fetch_array($result2)) {
//the first element of the returned array will always be the table name, so:
$query = "select * from ".$row2[0]." where 1=1;";
$result3 = mysql_query($query);
echo "Query:\t(".$row[0].'/'.$row2[0].")$query\n";
//If mysql_query returns false (i.e., $result3 is false), that means that
// the table is damaged
if(!$result3) {
echo "***Error on table '".$row2[0]."' *** ... Fixing...";
//So, we repair the table
mysql_query("repair table ".$row2[0].";");
}
}
}
?>