Invalid object name [UserInfo_Table_Azure] - php

I'm using mobile service of Windows Azure to store data which retrieved from my windows phone application. In my project, I need to generate a pdf when a user needs on website. So I'm using PHP to generate a PDFs which I need to get data from a table in mobile service.
However, when I try to get data, I always receive this error from Windows Azure (SQL server?).
MySql Connected [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'UserInfo_Table_Azure'. Failed to query test table: SELECT Username FROM [UserInfo_Table_Azure]
This is my code
$connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);
sqlsrv_configure('WarningsReturnAsErrors', 0);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if($conn === false)
{
FatalError("Failed to connect...");
} else {
echo "Sql Connected \n";
}
$tsql = "SELECT Username FROM [$table]";
$stmt = sqlsrv_query($conn, $tsql);
if ($stmt === false)
{
FatalError("Failed to query test table: ".$tsql);
}
else
{
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC))
{
echo "Col1: ".$row[0]."\n";
}
sqlsrv_free_stmt($stmt);
}
It always shows "Sql connected" but can't get data from the table.
Do you guys have any idea?
Thank you.

try the following code
if($conn == "false") {
FatalError("Failed to connect...");
} else {
echo "Sql Connected \n";
}
it will be 100% guarantee about connection success

Finally, I can find the solution. The problem is the schema of mobile service is not a default schema. The schema of tables in mobile service is set according to your mobile service name. For example, if my mobile service name is "School", what I have to do to get data in this mobile service is to use [School].[a table name] instead of just [a table name].

Related

I am not able to execute select records from a table in database in google sql cloud using php

<?php
define('DB_NAME', 'xp_database'); // DATABASE
define('DB_USER', 'valli'); // ROOT DEFAULT MYSQL
define('DB_PASSWORD', 'Valli123'); // PASSOWORD
define('DB_HOST', '127.0.0.1'); // LOCAL IF YOU USE LOCAL.
$data=array();
$i=1;
$link = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
else
{
echo "Connected to database";
}
// Attempt select query execution
$sql = "show databases";
if($result = mysqli_query($link, $sql))
{
if(mysqli_num_rows($result) > 0)
{
echo "<table>";
echo "<tr>";
echo "<th>userid</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['user_id'] . "</td>";
echo "</tr>";
// Free result
//set mysqli_free_result($result);
}
}
else
{
echo "No records matching your query were found.";
}
}
else
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
I have enabled the Admin API in google cloud.
Connection is successful. But retrieval using select query is not successful. Please tell me what is missing in this. Is there any other setting that I have to do. I have migrated the data from local database to mysql cloud database.
If you are using App Engine to connect to Cloud SQL, bear in mind that you will need to enable Cloud SQL proxy since Google App Engine connects from inside of Google servers to your Cloud SQL instance via proxy.
Nonetheless, if what you would like to do is connect from outside with pure PHP code, you can still do it without using the Cloud SQL proxy.
For connecting from outside using pure PHP code you will need to authorize your IP from your Cloud SQL instance, as shown here.
Then, you should modify your code in order to change localhost or 127.0.0.1 to your Cloud SQL instance public IP (taking into account the other variables like username, password and the database name that you would like to connect to).
For finding your Cloud SQL's public IP, you can refer to here.
Finally, if you would like to look around PHP on Google App Engine, you can do it by checking the documentation here.
I hope this helps.

Connect Laravel with vertica Database

I have problems linking my Project to my database. I want to select stuff from a vertica database into my project. In normal PHP it works, but the same code does not work in Laravel:
# Connect to the Database
$dsn = "VerticaDSN";
$conn = odbc_connect($dsn,'','') or die ("<br/>CONNECTION ERROR");
echo "<p>Connected with DSN: $dsn</p>";
# Get the data from the table and display it
$sql = "SELECT column FROM table";
if($result = errortrap_odbc($conn, $sql))
{
echo "<pre>";
while($row = odbc_fetch_array($result) )
{
echo "hi";
print_r($row);
}
echo "</pre>";
}
Is there a method that this code works inside my Laravel Controller?
What exact error have you? Is your $dns correct? It must look like:
$dsn = 'Driver=Vertica;Servername=xxx;Port=5433;Database=yyy';
odbc_connect($dsn,'usr','pwd');
In my Github repository there are detailed instructions for the use of Vertica in Laravel.

Can't Connect to local server using PDO?

I have been playing around for PHP with a while. This is how i connect to sql server and it works:
$serverName = "SNAME";
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"USERNAME", "PWD"=>"PASSWORD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$sql = "SELECT X FROM myTable";
$query = sqlsrv_query($conn,$sql);
if ($query === false){
echo "Could not link to SQL Server";
}
while ($row = sqlsrv_fetch_array($query))
{
$ARRAY[] = "$row[X]";
}
Server SNAME is on the local network, and i can connect to it from my computer using Sql Management Studio.
Now looking online and other places I have realized PDO is the better way to go. So I have been trying to connect to my server using PDO, by following examples online. Here is the code:
$username = "USERNAME";
$password = "PASSWORD";
try{
$conn = new PDO('mysql: host=SNAME;port=1433;dbname=DBNAME',$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = $conn->query('SELECT X FROM myTable');
foreach($data as $row) {
print_r($row);
}
}
catch{
echo 'ERROR: ' . $e->getMessage();
}
When i run this i get this on my page:
ERROR: SQLSTATE[HY000] [2002] No connection could be made because the
target machine actively refused it.
Now i know my other information is right since i can use the top block and sql management studio. Is there anyother library or something special needed. Is there problem with my code?
UPDATE
I ran this in my SQL Management Studio to check my port and it gave me 1433.
SELECT DISTINCT local_tcp_port FROM sys.dm_exec_connections WHERE local_tcp_port IS NOT NULL
In one example, you are using sqlsrv_connect, but in the other you tell PDO to connect to a 'mysql:' database.
You need to use the correct DSN string for a SQL Server connection.
$conn = new PDO('sqlsrv:Server=SNAME,1433;Database=DBNAME',$username,$password);
Manual: http://php.net/manual/en/ref.pdo-sqlsrv.connection.php
P.S. Make sure you have PDO_SQLSRV installed. http://php.net/manual/en/ref.pdo-sqlsrv.php

Get data from a table in Azure database

I want to get data from the table I have created in Microsoft Azure.
$conn = sqlsrv_connect($server, array("UID"=>$user, "PWD"=>$pwd, "Database"=>$db));
When I connect with the code above, it works. Because I put conditions
if($conn === false){
die(print_r(sqlsrv_errors()));
}
if($conn == true){
echo "hi!";
}
and it returns true and prints "hi!".
But when I try to get data from the table course, it fails.
$cek=sqlsrv_query($conn, "SELECT ID FROM course");
Because after the $cek query, I put such control;
echo "<pre>";
print_r($cek);
echo "</pre>";
if(!$cek)
{echo "Fail!";}
and it gives such error;
Resource id #2
Fail!
How can I manage this problem?
Thank you for your help.
The sqlsrv_query function returns a statement resource.
Returns a statement resource on success
So we need to extract the data from the resource using a function like sqlsrv_fetch_array
$data = sqlsrv_fetch_array($cek);
var_dump($data);

Insert data into MySql table strange exception

I am using this code to insert some values in MySql table:
<?php
mysql_connect("localhost","root","root");
mysql_select_db("bib");
$id = "12";
$titlu = "Joe";
$query = "INSERT INTO carte SET id='$id', titlu='$titlu'";
$result = mysql_query($query);
// Display an appropriate message
if ($result)
echo "<p>Product successfully inserted!</p>";
else
echo "<p>There was a problem inserting the Book!</p>";
mysql_close();
?>
After running it into browser, the following error occurs:
"Apache HTTP Server has encountered a problem and needs to close. We are sorry for the inconvenience."
It seems that mysql_select_db("bib") statement causes it. Database is create , also table...
I am running php 5.3 and mysql 5.1 on windows xp sp 2.
Please any ideas are welcomed...
Thanks...
Any of the mysql_* functions can fail for various reasons. You have to check the return values and if a function indicates an error (usually by returning FALSE) your script has to react appropriately.
mysql_error($link) and mysql_errno($link) can give you more detailed information about the cause. But you don't want to show all the details to just any arbitrary user, see CWE-209: Information Exposure Through an Error Message.
If you don't pass the connection resource returned by mysql_connect() to subsequent mysql_* functions calls, php assumes the last successfully established connection. You shouldn't rely on that; better pass the link resource to the functions. a) If you ever have more than one connection per page you must pass it anyway. b) If there is no valid db connection the php-mysql modules tries to establish the default connection which is usually not what you want; it only takes up more time to fail ..again.
<?php
define('DEBUGOUTPUT', 1);
$mysql = mysql_connect("localhost","root","root");
if ( !$mysql ) {
foo('query failed', mysql_error());
}
$rc = mysql_select_db("bib", $mysql);
if ( !$rc) {
foo('select db', mysql_error($mysql));
}
$id = "12";
$titlu = "Joe";
$query = "INSERT INTO carte SET id='$id', titlu='$titlu'";
$result = mysql_query($query, $mysql);
// Display an appropriate message
if ($result) {
echo "<p>Product successfully inserted!</p>";
}
else {
foo("There was a problem inserting the Book!", mysql_error($mysql), false);
}
mysql_close($mysql);
function foo($description, $detail, $die=false) {
echo '<pre>', htmlspecialchars($description), "</pre>\n";
if ( defined('DEBUGOUTPUT') && DEBUGOUTPUT ) {
echo '<pre>', htmlspecialchars($detail), "</pre>\n";
}
if ( $die ) {
die;
}
}
try this to connect to database:
$mysqlID = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die("Unable to connect to database");
mysql_select_db(DB_DATABASE) or die("Unable to select database ".DB_DATABASE);
also, try this as your insert query:
$query = "INSERT INTO carte (id, title) values ('".$id."', '".addslashes($titlu)."')
$result = mysql_query($query) or die(mysql_error());
By using die(), it will tell you where it has failed and why

Categories