display stored procedure on my website (php, mssql) - php

I'm whole new to stored procedures and mssql so i have no idea where to start with my code. I've tried to google/search around the forum but i dont understand the code.
Here is my scenario:
I've got the name of some stored procedures that i'm trying to display on my website using php. I have the name of the server(mssql), i've got the name of the database and i have a username and password to the server.
I've used Toad and tried the stored procedures(with success) and now im just trying to put together some code where i can display the results of the stored procedures on my website.
Firstly i've tried to just display the results of a stored procedure without parameters but nothing happens...
<?php
$server = "myServername";
$username = "myUsername";
$password = "myPassword";
$database = "myDatabase";
$connect = mssql_connect($server, $username, $password) or die ("Couldn't connect to SQL Server");
mssql_select_db($database, $connect) or die ("Couldn't open database");
$query = mssql_init("usp_ThisIsMyStoredProcedure", $connect);
$result = mssql_execute($query);
while ($row = mssql_fetch_row($result)) {
echo "<li>" . $row[0] . "</li>";
}
?>
My website only displays: -->" . $row[0] . ""; } ?> <--
please feel free to come with tips on how i can restructure my code!

(don't have enough reputation to comment your post so i post here).
Shouldn't you add the value you want ?
Like
echo "<li>" . $row[0]->{'id'} . "</li>";
echo "<li>" . $row[0]->{'nickname'} . "</li>";
for example ...

Related

No database selected and i don't know what wrong with my code, i already change multiple way but still get same result. "No database" [duplicate]

This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 3 years ago.
Hye, i am new in PHP, i have error that can't connect with database, can anyone look at my code if i miss what code i was wrong.
<?php
$dbconnection = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbdatabase = "aduan";
$conn = mysql_connect($dbconnection, $dbusername, $dbpassword, $dbdatabase);
// Check connection
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM unsafeact";
$result = mysql_query($sql,$conn) or die(mysql_error());
// output data of each row
while($row = mysql_fetch_assoc($result)) {
echo "<tr><td>" . $row["PEEViolation"]. "</td><td>" . $row["IgnoringSafetySignboard"] . "</td><td>"
. $row["RuleViolation"] . $row["WrongToolEquipmentMachinery"] . "</td><td>" . $row["ImproperMaterialHandling"] . "</td><td>" . $row["UseFaultyToolsEquipmentVehicle"] . "</td><td>" . $row["PositiveFinding"] . "</td><td>" . $row["Others1"] . "</td></tr>";
}
mysql_close($conn);
?>
P/S: my company still using old code which is mysql, not mysqli. So i don't know what wrong in my code that can make it cannot connect with database. I'm really appreciate with your kindness. Thank you
According to PHP manual for mysql_connect() the 4th parameter is not used for identify the database name but new_link:
If a second call is made to mysql_connect() with the same arguments,
no new link will be established, but instead, the link identifier of
the already opened link will be returned. The new_link parameter
modifies this behavior and makes mysql_connect() always open a new
link, even if mysql_connect() was called before with the same
parameters. In SQL safe mode, this parameter is ignored.
to select database using mysql_*, you have to use mysql_select_db()
$dbconnection = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbdatabase = "aduan";
$conn = mysql_connect($dbconnection, $dbusername, $dbpassword);
mysql_select_db($dbdatabase, $conn);
Ideally, you don't want to use mysql_* anymore since it deprecated and removed from php7.
$sql = "SELECT * FROM aduan.unsafeact";
try this, or make a condition if your code is connected in your database.
sorry if wrong. tnx :D

mysqli_select_db wrong database

I'm trying to move a web page from one sever to another which I'm not the author (some kind of custom cms).
My problem with it is that I prints only the message Wrong database. I did check the login data with login through phpmyadmin and they work.
On the old server there was a version of php 5.4 and on the new one I have the 7.0 version
<?php
$DB->host = 'localhost';
$DB->name = 'db-name';
$DB->pass = 'pass';
$DB->login = 'db-user';
$DB->prefix = '';
$System->files = '';
$System->root = '';
$System->lang['pl'] = 'Polski';
$System->lang['de'] = 'Deutsch';
$System->lang['en'] = 'English';
//$System->lang['ru'] = 'русский';
$System->component->articles = true;
$System->component->references = true;
$System->component->contakt = true;
$link = mysqli_connect($DB->host,$DB->login,$DB->pass) or die ("Can't connect: " . mysqli_error());
mysqli_select_db($DB->name, $link) or die ("Wrong database: " . mysqli_error());
mysqli_query("SET NAMES 'utf8'");
unset($link,$DB->name,$DB->login,$DB->pass);
?>
When using procedural style mysqli_select_db the first parameter is the link identifier then comes the database name
mysqli_select_db( $link, $DB->name );
See the documentation.
Worth mentioning you have also other issues in your code: see Barmar's comments.
I suggest you check the manual for every mysqli function you're using; you should be able to fix things easily.

PHP connection to MS SQL

I'm trying to connect my PHP webpage with my MS SQL database. I've got this code from the internet, but have tried others. All seems to come back with a problem with "mssql_connect".
I have tried (I think) everything and can not find out why it won't work.
My code is:
<?php
$myServer = 'SQL5008.Smarterasp.net,1433';
$myUser = '*****';
$myPass = '*****';
$myDB = '*****';
//connection to the database
$dbhandle = mssql_connect($myServer, $myuser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT id ";
$query .= "FROM tblEmployees ";
$query .= "WHERE CompanyID=3";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
Why dont you try using PDO. I use it with SQL SERVER daily. You will need the php sqlsrv extenstion though.
// instantiate the pdo object
try {
$Server = "localhost";
$User = "username";
$Pass = "password";
$Database = "mydb";
$this->conn = new PDO("sqlsrv:Server=$Server;Database=$Database", $User, $Pass);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//allow for SQL query errors
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Check using phpinfo() if mssql extension is loaded. According to PHP Manual:
This extension is not available anymore on Windows with PHP 5.3 or later.
If you find out mssql is unloaded, try to connect using sqlsrv extension. Here you can find a few examples http://php.net/manual/ru/function.sqlsrv-connect.php
Anyway it's a good idea to post here an error message you get from PHP.

PHP cannot connect to MS SQL Server 2008 IIS

I did an express install for MS SQL Server 2008 and created a database with a test table. I am running Windows Server 2008 and have IIS, PHP and MS SQL installed. I chose to go with SQL server authentication, where you would need credentials to modify or view the database. I try to print out a simple test table on a web page, but it keeps failing and only returns a white screen..
<?php
$server = "WS1\SQLEXPRESS";
$username = "sa";
$password = "password";
$db = "testdb1";
$dbhandle = mssql_connect($server, $username, $password)
or die ("Cannot connect to SQL Server on $server");
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
echo "You are connected to the " . $db . "database on the " . $server . ".";
$query = "SELECT * FROM table1";
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
echo "<li>" . $row[""] . $row[""] . "</li>";
while ($row = mssql_fetch_array($result)) {
print_r($row);
}
mssql_close($dbhandle);
?>
Ah, the horrific white screen. Sometimes very hard to troubleshoot. Often times it's a simple missing semicolon ...
like the one here:
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
Put a semicolon on the end of that statement and you are back in business.

Mysql call stops all forms of output php

Hello, I'm kind of new to php, so don't bash on me, but I just can't figure out what the problem is with this code. So basically I have several forms of output, but as soon as I do anything with mysql ( even just connect and disconnect! ), it won't allow me to do any kind of output. It also won't allow me to redirect.
I tried taking all the code out between the mysql connect and disconnect code and it didn't help to resolve anything, However, as soon as I comment out the mysql connection code, all my outputs and redirects work! I'm trying to build a simple login script that gets the email and password from a form elsewhere. I would love to get this resolved so I could figure out if the rest of it works. And I know that 'header' will not work after echo; the echo and the file writes will not be there as soon as I can make sure this is working. Any help would be appreciated! Thanks!
<?php
/*
Login.php searches for the email address given by loginPage.php in the data base.
If the email is found and the password given by loginPage.php matches that stored
in the data base, a session will begin and the client will be redirected to the
main page.
*** INCOMPLETE ***
*/
echo "HELLO!";
$email = $_POST["email"];
$password = $_POST["password"];
$errorLog = fopen("login.txt", "w");
fwrite($errorLog, "***Sesion started***");
$mysql_id = mysql_connect("localhost", "root", "12131");
if (!$mysql_id)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('informationStation', $mysql_id);
$results = mysql_query("SELECT password FROM Personals WHERE email = '" . $email . "';", $mysql_id);
if($results != null && $password == mysql_fetch_array($result))
{
$redirect = 'Location: http://127.0.1.1/main.php';
}
else
{
$redirect = 'Location: http://127.0.1.1/loginPage.php';
{
mysql_close($mysql_id);
fwrite($errorLog, "result: " . $results);
fwrite($errorLog, "redirect: " . $redirect);
fclose($errorLog);
header($redirect);
?>
Try this to get you started..
$results = mysql_query("SELECT password FROM Personals WHERE email = '" . $email . "'", $mysql_id) or die(mysql_error());
But you also need to read about sql injection.
And you should not store passwords in your database without salting and hashing them.
And you also need to use array syntax to access the data from your result...
$mysql = mysql_connect("localhost", "root", "12131") or die('Could not connect: ' . mysql_error());
mysql_select_db('informationStation', $mysql);
function loged($email, $password) {
$result = mysql_query("SELECT id FROM Personals WHERE email = '" . $email . "' AND password='" . $password . "'");
if(mysql_num_rows($result) != 1)
return false;
return true;
}
if(loged(mysql_real_escape_string($email), md5($password))) {
header('Location: http://127.0.1.1/mainPage.php');
exit;
}
header('Location: http://127.0.1.1/loginPage.php');
In this example you need to store users password using md5 encryption method (search for other more securely encryption methods).
Also we've escaped the email address against sql injection.
I've created a function which can be called every time you want to see if the user is loged in or not.
Note that this is not a complete login script. You will also need to make a login function where you'll have to start a new session for each user.

Categories