Script printing Blob works on one hosting site, not on another - php

I've a script that prints images (Blobs from mysql DB). It works fine on Godaddy but when I move it to a UK hosting company it fails to print the image - prints the alt text instead (everything else works fine). GD is PHP 5.3 and the UK company is PHP 5.2. Both mysql DBs are 5. I have checked that the scripts are the same, correct data is in the DB etc.
I am at a loss as to where to look now.
The script is called with
echo "<img src = 'printimage1.php?recipetableID=$recipetableID' alt='Picture does not display.'>";
and the printimage1.php script looks like
<?php
header("Content-type: image/jpg");
$recipetableID=$_GET['recipetableID'];
include("connect.inc");
$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");
$query = "SELECT * FROM RecipeTable WHERE recipetableID = '$recipetableID'";
$result = mysql_query($query)
or die ("Couldn't execute query.");
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
echo $row['Picture1content'];
}
?>
Any suggestions appreciated.
John.

Related

a lot of mysql DB disconnect

I have website I was build it from scratch using PHP/MYSQL but unfortunately I got a lot of mysql DB disconnect "not conect", I'm sure about DB Name, user and Password .. My config file below :
$conectdb = mysql_connect("localhost","user","pass") or die ("not conect");
mysql_set_charset('utf8',$conectdb);
$selectdb = mysql_select_db("DB_name",$conectdb)or die ("selected DB");
$charset = mysql_client_encoding($conectdb);
header('X-Frame-Options: SAMEORIGIN');

phpgraph lib not showing any output

Im using phpgraph lib to create graphs on my linux server. I tried an example and it worked, but I had provided it with the data.
then I wanted to connect it to mysql database and plot a query, when I run it, nothing happens, I don't see any output on the page or any errors, I don't see any output on the page at all, even if I put wrong credentials to my database e.t.c any inputs?
I have executed the sql statement on sql server and it's working fine.
the version of php the server has is PHP 5.3.3
<?php
include('phpgraphlib.php');
$graph= new PHPGraphLib(550,350);
$link = mysql_connect('localhost', 'user', 'password')
or die('Could not connect: ' . mysql_error());
mysql_select_db('databasename' or die('Could not select database');
$dataArray=array();
//get data from database
$sql="my sql statement";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$salesgroup=$row["var1"];
$count=$row["count"];
//add to data areray
$dataArray[$salesgroup]=$count;
}
}
//configure graph
$graph->addData($dataArray);
$graph->setTitle("Sales by Group");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>
I fixed it, I was expecting to see errors on the webpage, but didn't see any on CHROME, I hen opened it in IE and saw error 500.
Troubleshooted through the log file.
Turned out the sql statement wasn't suppose to have double quotes e.g instead of
where name="john"
it's suppose to be
where name='john'

MAMP running, PHP not

Calling a php file from a simple HTML form - e.g., - get php source code in browser as result (see below).
Figured issue is that PHP is not running on localhost (using older version of MAMP). Checked, double-checked, and upgraded to latest MAMP PRO version (2.1.4).
Still have the same problem - PHP & Apache configs look correct.
Thoughts? (guessing that first suggestion will be to ditch MAMP.)
PHP:
<?php
//Open a database connection
$con = mysqli_connect('localhost', 'root', 'root') or die( mysql_error() ); mysql_select_db($database) or die( mysql_error() );
//Declare variables for form data
$toDoItem = mysql_real_escape_string ($_POST["toDoItem"]);
$toDoDue = mysql_real_escape_string ($_POST["toDoDue"]);
$toDoOwner = mysql_real_escape_string ($_POST["toDoOwner"]);
//Query
$sql = "INSERT INTO toDo
(toDoItem, toDoDue, toDoOwner)
VALUES ($toDoItem, $toDoDue, $toDoOwner)";
$result = mysql_query($sql) or die( mysql_error() );
if($result){
echo ("<br>Data input successful.");
} else {
echo ("<br>Data input failed.");
}
//close the connection
mysql_close();
?>
Do you get the MAMP screen when you go to localhost? I think that if you get the screen but click on PhpInfo it will also print out regular php statements.
It seems that it's because of a faulty install

My php script is not using given username/pass/host rather using root#localhost (password: NO)

Got a problem! Though I found almost similar threads but none helped :(
I've written a php script to fetch the number of registered users from my MySQL database. The script is working great in my localhost; it is using the given username,pass and host name which are "root", "root", and "localhost" respectively, but the script is not using the given username/pass/host rather using root#localhost (password: NO) in Live server.
In the Live server I created a MySQL user, set an different password, and hostname there is of course not localhost. I updated the script with my newly created mysql users data. BUT, whenever I run the script, I see that the script is still using "root", "root", and "localhost"!!
take a look at the script:
//database connection
$conn = mysql_connect( "mysql.examplehost.com", "myusername", "mypass" );
$db = mysql_select_db ("regdb",$conn); //Oops, actually it was written this way in the script. I misstyped it previously. now edited as it is in the script.
//Query to fetch data
$query = mysql_query("SELECT * FROM regd ");
while ($row = mysql_fetch_array($query)):
$total_regd = $row['total_regd'];
endwhile;
echo $total_regd;
-- Some says to change the default username and pass in the config.ini.php file located in phpMyAdmin directory. Would this help?? I didn't try this because either my hosting provider didn't give me privilege to access that directory (because I am using free hosting for testing scripts) or I simply didn't find it :(
Please help....
Foreword: The MySQL extension is marked as deprecated, better use mysqli or PDO
Though you store the connection resource in $conn you're not using it in your call to mysql_query() and you're not checking the return value of mysql_connect(), i.e. if the connection fails for some reason mysql_query() "is free" to establish a new default connection.
<?php
//database connection
$conn = mysql_connect( "mysql.examplehost.com", "myusername", "mypass" );
if ( !$conn ) {
die(mysql_error()); // or a more sophisticated error handling....
}
$db = mysql_select_db ("regdb", $conn);
if ( !$db ) {
die(mysql_error($conn)); // or a more sophisticated error handling....
}
//Query to fetch data
$query = mysql_query("SELECT * FROM regd ", $conn);
if (!$query) {
die(mysql_error($conn)); // or a more sophisticated error handling....
}
while ( false!=($row=mysql_fetch_array($query)) ):
$total_regd = $row['total_regd'];
endwhile;
echo $total_regd;
edit: It looks like you're processing only one row.
Either move the echo line into the while-loop or (if you really only want one record) better say so in the sql statement and get rid of the loop, e.g.
// Query to fetch data
// make it "easier" for the MySQL server by limiting the result set to one record
$query = mysql_query("SELECT * FROM regd LIMIT 1", $conn);
if (!$query) {
die(mysql_error($conn)); // or a more sophisticated error handling....
}
// fetch data and output
$row=mysql_fetch_array($query);
if ( !$row ) {
echo 'no record found';
}
else {
echo htmlspecialchars($row['total_regd']);
}
First of all:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
What is your mysql_error()? :)

php script connecting to my SQL database

When trying to connect to my database using PHP I am getting an error.
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
mysql_select_db("databasename") or die ("Couldnt find database");
When I try running the script it keeps saying 'Couldnt find database' which means that I am connecting to the server but my database won't connect.
Am I doing something wrong here? I have pretty much copied and pasted my database name from Cpanel so I know there aren't any mistakes. But in Cpanel it is displayed as 'mywebsite'_database
Any ideas?
You specify in your question that your database is called:
'mywebsite'_database
If this is the case, then you need to change your code to
// Connect to the database
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
// Select the database
mysql_select_db("mywebsite_databasename",$connect) or die ("Couldnt find database");
If the above method does not work then I would advise debugging your connection by listing the databases for that particular user. Do this like so:
// Connect to the database
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
// Get all the databases for this particular user
$databases = mysql_list_dbs($connect);
// Loop through the databases and write them on the screen
while($database = mysql_fetch_assoc($databases)) {
echo $database['Database']."\n";
}
exit;
mysql_select_db("mywebsite_database")
Print out the names of all the databases in the server (using mysql_list_dbs).
Find the correct database name and replace "databasename" with it.
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
$db_list = mysql_list_dbs($connect);
while ($row = mysql_fetch_object($db_list)) {
echo $row->Database . "\n";
}
mysql_select_db("databasename", $connect) or die ("Couldnt find database");
I recommend using MySQLi extension as mysql_* is currently in a depreciation process.
I hope this helps

Categories